将相对 URL 转换为绝对 URL [英] Converting relative URL to absolute
问题描述
假设我有一个链接到另一个文档的文档的 URL(可以是绝对的,也可以是相对的),我需要这个链接是绝对的.
Let's say I have a URL of the document linking to another document (which can be both absolute or relative) and I need to have this link in absolute.
我为几种常见情况制作了提供此功能的简单函数:
I made the simple function providing this functionality for several common cases:
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
不起作用.
The code works fine, but there could be more cases such as ../../path/to/file.php
which will not work.
那么有没有比我的函数做得更好(更通用)的标准类或函数?
So is there any standard classes or function doing this thing better (more universal) that my function?
我尝试使用 Google 搜索并查看类似的问题(one 和
I tried to Google it and check the similar questions (one and two) but it looks like server-path related solution which is not the thing I'm looking for.
推荐答案
此函数将解析相对 URL 到 $pgurl
given 当前页面的 url正则表达式.它成功解析:
This function will resolve relative URLs to a given current page url in $pgurl
without regex. It successfully resolves:
/home.php?example
类型,
same-dir nextpage.php
类型,
same-dir nextpage.php
types,
../.../.../parentdir
类型,
完整的 http://example.net
网址,
和速记 //example.net
urls
//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'));
nodots()
必须在 URL 转换为绝对后调用.
nodots()
must be called after the URL is converted to absolute.
dots 函数有点多余,但可读、快速、不使用正则表达式,并且将解析 99% 的典型 url(如果你想 100% 确定,只需扩展 switch 块以支持 6+点,尽管我从未在 URL 中看到过这么多点).
The dots function is kind of redundant, but is readable, fast, doesn't use regex's, and will resolve 99% of typical urls (if you want to be 100% sure, just extend the switch block to support 6+ dots, although I've never seen that many dots in a URL).
希望这会有所帮助,
这篇关于将相对 URL 转换为绝对 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!