Php 将相对URL转换为绝对URL

Php 将相对URL转换为绝对URL,php,url,normalization,Php,Url,Normalization,假设我有一个链接到另一个文档的文档URL(可以是绝对的,也可以是相对的),我需要将这个链接设置为绝对的 我制作了一个简单的函数,为几种常见情况提供了此功能: function absolute_url($url,$parent_url){ $parent_url=parse_url($parent_url); if(strcmp(substr($url,0,7),'http://')==0){ return $url; } elseif(strcmp(substr($ur

假设我有一个链接到另一个文档的文档URL(可以是绝对的,也可以是相对的),我需要将这个链接设置为绝对的

我制作了一个简单的函数,为几种常见情况提供了此功能:

function absolute_url($url,$parent_url){
  $parent_url=parse_url($parent_url);
  if(strcmp(substr($url,0,7),'http://')==0){
    return $url;
  }
  elseif(strcmp(substr($url,0,1),'/')==0){
    return $parent_url['scheme']."://".$parent_url['host'].$url;
  }
  else{
    $path=$parent_url['path'];
    $path=substr($path,0,strrpos($path,'/'));
    return $parent_url['scheme']."://".$parent_url['host']."$path/".$url;
  }
}

$parent_url='http://example.com/path/to/file/name.php?abc=abc';
echo absolute_url('name2.php',$parent_url)."\n";
// output http://example.com/path/to/file/name2.php
echo absolute_url('/name2.php',$parent_url)."\n";
// output http://example.com/name2.php
echo absolute_url('http://name2.php',$parent_url)."\n";
// output http://name2.php
代码可以正常工作,但是可能还有更多的情况,例如
。/../path/to/file.php
不起作用

那么有没有标准类或函数比我的函数做得更好(更通用)

我试着用谷歌搜索它,并检查类似的问题(和),但它看起来像是服务器路径相关的解决方案,这不是我正在寻找的东西

$uri = "..";
$path = realpath($uri);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);

if($path){
    $path = str_replace($root, "", $path);
    $path = $_SERVER["SERVER_NAME"] . $path;
    $protocol = "http";
    if(isset($_SERVER["HTTPS"])){
        $protocol .= "s";        
    }
    $path = "{$protocol}://$path";
    $path = str_replace("\\", "/", $path);
}

var_dump($path);

可能有更好/更快的方法,但我只是把它搞砸了

此函数将解析相对url到
$pgurl
中给定的当前页面url,而不使用regex。它成功地解决了:

/home.php?示例
类型

相同的目录
nextpage.php
类型

。/…/…/parentdir
类型

完整<代码>http://example.netURL

和速记
//示例.net
URL

//Current base URL (you can dynamically retrieve from $_SERVER)
$pgurl = 'http://example.com/scripts/php/absurl.php';

function absurl($url) {
 global $pgurl;
 if(strpos($url,'://')) return $url; //already absolute
 if(substr($url,0,2)=='//') return 'http:'.$url; //shorthand scheme
 if($url[0]=='/') return parse_url($pgurl,PHP_URL_SCHEME).'://'.parse_url($pgurl,PHP_URL_HOST).$url; //just add domain
 if(strpos($pgurl,'/',9)===false) $pgurl .= '/'; //add slash to domain if needed
 return substr($pgurl,0,strrpos($pgurl,'/')+1).$url; //for relative links, gets current directory and appends new filename
}

function nodots($path) { //Resolve dot dot slashes, no regex!
 $arr1 = explode('/',$path);
 $arr2 = array();
 foreach($arr1 as $seg) {
  switch($seg) {
   case '.':
    break;
   case '..':
    array_pop($arr2);
    break;
   case '...':
    array_pop($arr2); array_pop($arr2);
    break;
   case '....':
    array_pop($arr2); array_pop($arr2); array_pop($arr2);
    break;
   case '.....':
    array_pop($arr2); array_pop($arr2); array_pop($arr2); array_pop($arr2);
    break;
   default:
    $arr2[] = $seg;
  }
 }
 return implode('/',$arr2);
}
用法示例:

echo nodots(absurl('../index.html'));
将URL转换为绝对URL后必须调用
nodots()

dots函数有点冗余,但可读性强,速度快,不使用正则表达式,可以解析99%的典型URL(如果你想100%确定,只需扩展开关块以支持6+个点,尽管我从未见过URL中有这么多点)


希望这有帮助,

。\..\path\to\file.php
是一个文件路径,而不是url,因此需要一个单独的函数。@Steve so
在HTML文档中不起作用?它可能会起作用,具体取决于浏览器,但不能保证。uri(您在链接中放置的内容)应该使用前斜杠。我并不想显得尴尬,我真的以为你在谈论文件路径,例如用于phps include等@Steve oh!你说得对!我说的是URI。因此,我的意思是
。/../path/to/file.php
这是我需要的用于远程文件的函数的副本,因为我知道此函数仅适用于本地文件。对吧?是的。。。我明白了,抱歉:)没有测试,但看起来正是我需要的。