PHP收罗链接,相对链接转为绝对链接

这个收罗链接是从Snoopy中提取出来的,也是一个很好的函数,可以按照URL是相对链接照旧绝对链吸收罗到链接,假如是相对链接会按拍照对链接和主域名,返谢绝对链接,也支持差异端口。

<?php /*===================================================================*\ Function: _expandlinks Purpose: expand each link into a fully qualified URL Input: $links the links to qualify $URI the full URI to get the base from Output: $expandedLinks the expanded links \*===================================================================*/ function _expandlinks($links,$URI) { $URI_PARTS = parse_url($URI); $host = $URI_PARTS["host"]; preg_match("/^[^\?]+/",$URI,$match); $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]); $match = preg_replace("|/$|","",$match); $match_part = parse_url($match); $match_root = $match_part["scheme"]."://".$match_part["host"]; $search = array( "|^".preg_quote($host)."|i", "|^(\/)|i", "|^(?!)(?!mailto:)|i", "|/\./|", "|/[^\/]+/\.\./|" ); $replace = array( "", $match_root."/", $match."/", "/", "/" ); $expandedLinks = preg_replace($search,$replace,$links); return $expandedLinks; } //以下是测试内容 $r = _expandlinks('asd/asd.html','http://enenba.com/'); echo $r; //output echo '<br />'; $r = _expandlinks('http://enenba.com/asd.html','http://enenba.com/'); echo $r; //output echo '<br />'; $r = _expandlinks('http://enenba.com/asd.html','http://enenba.com:8080/'); echo $r; //output :8080/asd.html ?>

颠末测试,可以知道

第一个参数$links是链接的url

较量你采到网站中链接是<a href="http://enenba.com/asd.html">测试</a>

主站域名是 此函数会按拍照对路径干系,反谢绝对路径

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7929.html