假设我们有两个变量:
$base = "http://some.base.url/script.php?query=string";
$link = "./anotherScript.php?query=anotherString";
PHP中有任何方法将这些URL零件组合成:
$result = "http://some.base.url/anotherScript.php?query=anotherString";
?
您可以将parse_url函数用于parse base url也可以使用$ link的str_replace。
示例;
$base = "http://some.base.url/script.php?query=string";
$link = "./anotherScript.php?query=anotherString";
$scheme = parse_url($base);
$link = str_replace('./','/',$link);
echo $scheme["scheme"]."://".$scheme["host"].$link;
它将提供回声http://some.base.url/anotherscript.php?query = anothersTring
我在自己的项目中使用此功能。
public static function uri($base, $com)
{
// check base
if(preg_match('/(https?|ftp|tcp|udp)://([w]+.)?([w]{4,})((.w{2,5}){1,2})/', $base)){
$base = parse_url($base);
if(preg_match('/^//', $com)){
$base['path'] = $com;
unset($base['query']);
}elseif (preg_match('/^.//', $com)) {
$base['path'] = strrev(preg_replace('/^[ws-_.]+//',"",strrev($base['path'])));
$base['path'] .= substr($com, 1);
unset($base['query']);
}else if(parse_url($com , PHP_URL_QUERY) != null){
if(isset($base['query'])){
$base['query'] .= '&'.substr($com, 1);
}else{
$base['query'] = substr($com, 1);
}
}
if(!isset($base['query'])) return $base["scheme"]."://".$base["host"].$base['path'];
else return $base["scheme"]."://".$base["host"].$base['path'].'?'.$base['query'];
}else {
return false;
}
}
示例输出:
var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","/index.php"));
// string(35) "http://globalmedia.com.tr/index.php"
var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","/index.php?deneme=test"));
// string(47) "http://globalmedia.com.tr/index.php?deneme=test"
var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","?add=test"));
// string(54) "http://globalmedia.com.tr/deneme.php?a=asdasd&add=test"
var_dump(Combine::uri("http://example.com/deneme/asdasd","?a=asdasd"));
// string(48) "http://globalmedia.com.tr/deneme/asdasd?a=asdasd"
var_dump(Combine::uri("http://example.com/deneme/asdasd.php","./index.php"));
// string(42) "http://globalmedia.com.tr/deneme/index.php"
php.net在对我有用的parse_url函数的注释部分中具有示例函数:https://www.php.net/manual/en/function.parse-url.php
有用的示例将看起来像这样:
/**
* Resolve a URL relative to a base path. This happens to work with POSIX
* filenames as well. This is based on RFC 2396 section 5.2.
*/
function resolve_url($base, $url) {
if (!strlen($base)) return $url;
// Step 2
if (!strlen($url)) return $base;
// Step 3
if (preg_match('!^[a-z]+:!i', $url)) return $url;
$base = parse_url($base);
if ($url{0} == "#") {
// Step 2 (fragment)
$base['fragment'] = substr($url, 1);
return $this->unparse_url($base);
}
unset($base['fragment']);
unset($base['query']);
if (substr($url, 0, 2) == "//") {
// Step 4
return $this->unparse_url(array(
'scheme'=>$base['scheme'],
'path'=>$url,
));
} else if ($url{0} == "/") {
// Step 5
$base['path'] = $url;
} else {
// Step 6
$path = explode('/', $base['path']);
$url_path = explode('/', $url);
// Step 6a: drop file from base
array_pop($path);
// Step 6b, 6c, 6e: append url while removing "." and ".." from
// the directory portion
$end = array_pop($url_path);
foreach ($url_path as $segment) {
if ($segment == '.') {
// skip
} else if ($segment == '..' && $path && $path[sizeof($path)-1] != '..') {
array_pop($path);
} else {
$path[] = $segment;
}
}
// Step 6d, 6f: remove "." and ".." from file portion
if ($end == '.') {
$path[] = '';
} else if ($end == '..' && $path && $path[sizeof($path)-1] != '..') {
$path[sizeof($path)-1] = '';
} else {
$path[] = $end;
}
// Step 6h
$base['path'] = join('/', $path);
}
// Step 7
return $this->unparse_url($base);
}
public function unparse_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
<?php
$url = explode('/', $base);
array_pop($url);
$result = implode('/', $url).substr($link, 1);;
?>