PHP的相对URL绝对URL转换与最终基地HREF HTML标记 [英] php relative urls to absolute urls conversion with eventually base href html tag
问题描述
我有一个加载了DOM的页面,然后我想根据最终的< base href>
标记将所有锚点的相对URL转换为绝对URL
我正在寻找经过测试的东西,而不是某些在某些情况下失败的随机脚本
我是有兴趣分析每种形式的href =用法:
href =relative.php
$ p
href = /absolute1.php
href =./ relative.php
href =../ relative.php
href =// absolutedomain.org
HREF = 相对
href =..相对
href =../相对
href =./相对
$ b 和更复杂的混合
预先感谢您
解决方案此函数将相对URL解析为给定当前页面url中的
$ pgurl
没有正则表达式。它成功地解决了:
/home.php?example
类型,
same-dir
nextpage.php
类型,
../ ..... / ... / parentdir
类型,
完整
http://example.net
网址,
和简写
// example.net
网址//当前基址(您可以从$ _SERVER动态检索)
$ pgurl ='http://example.com/scripts/ PHP / absurl.php';
函数absurl($ url){
global $ pgurl;
if(strpos($ url,'://'))return $ url; //已经绝对
if(substr($ url,0,2)=='//')return'http:'。$ url; //简写计划
if($ url [0] =='/')return parse_url($ pgurl,PHP_URL_SCHEME)。'://'.parse_url($ pgurl,PHP_URL_HOST)。$ url; //添加域
if(strpos($ pgurl,'/',9)=== false)$ pgurl。='/'; //如果需要的话,加斜杠到域
return substr($ pgurl,0,strrpos($ pgurl,'/')+ 1)。$ url; //获取相对链接,获取当前目录并追加新文件名
}
函数nodots($ path){//解决dot dot斜线,无正则表达式!
$ arr1 = explode('/',$ path);
$ arr2 = array();
foreach($ arr1 as $ seg){
switch($ seg){
case'。':
break;
case'..':
array_pop($ arr2);
休息;
case'...':
array_pop($ arr2); array_pop($ ARR2);
休息;
case'....':
array_pop($ arr2); array_pop($ ARR2); array_pop($ ARR2);
休息;
case'.....':
array_pop($ arr2); array_pop($ ARR2); array_pop($ ARR2); array_pop($ ARR2);
休息;
默认值:
$ arr2 [] = $ seg;
}
}
return implode('/',$ arr2);
$ / code>用法示例:
echo nodots(absurl('../ index.html'));
nodots()
必须被叫做 后URL转换为绝对。
点函数是多余的,但可读性强,速度快,不使用正则表达式,并且会解决99%的典型网址问题(如果你想100%确定,只需扩展开关块以支持6个点以上,尽管我从来没有在网址中看到过这么多点)。
希望这会有所帮助,
I've a page loaded with DOM, then I want to convert all relative URLs of anchors to absolute URLs according to, eventually, the
<base href>
tagI'm looking for something tested, not some random script that fails on some cases
I'm interested in parsing of every form of href="" usage:
href="relative.php" href="/absolute1.php" href="./relative.php" href="../relative.php" href="//absolutedomain.org" href="." relative href=".." relative href="../" relative href="./" relative
and more complex ones mixed
thank you in advance
解决方案This function will resolve relative URLs to a given current page url in
$pgurl
without regex. It successfully resolves:
/home.php?example
types,same-dir
nextpage.php
types,
../...../.../parentdir
types,full
http://example.net
urls,and shorthand
//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); }
Usage Example:
echo nodots(absurl('../index.html'));
nodots()
must be called after the URL is converted to absolute.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).
Hope this helps,
这篇关于PHP的相对URL绝对URL转换与最终基地HREF HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!