您的位置:首页 > 其它

插件21:把URL相对地址转换为绝对地址

2011-10-11 16:55 405 查看
<?php // Plug-in 21: Relative To Absolute URL

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

echo "<font face='Courier New' size='2'><pre>";
$page = "http://site.com/news/current/science/index.html";
$link = "../../prev/tech/roundup.html";
echo "Page: $page<br />Link: $link\n";
echo "Abs:  " . PIPHP_RelToAbsURL($page, $link);

$link = "/sport/index.htm";
echo "\n\nPage: $page<br />Link: $link\n";
echo "Abs:  " . PIPHP_RelToAbsURL($page, $link);

$page = "http://site.com/news/current/science/";
$link = "/sport/index.htm";
echo "\n\nPage: $page<br />Link: $link\n";
echo "Abs:  " . PIPHP_RelToAbsURL($page, $link);

$link = "../../prev/tech/roundup.html";
echo "\n\nPage: $page<br />Link: $link\n";
echo "Abs:  " . PIPHP_RelToAbsURL($page, $link);

function PIPHP_RelToAbsURL($page, $url)
{
// Plug-in 21: Relative To Absolute URL
//
// This plug-in accepts the absolute URL of a web page
// and a link featured within that page. The link is then
// turned into an absolute URL which can be independently
// accessed. Only applies to http:// URLs. The arguments
// required are:
//
//    $page: The web page containing the URL
//    $url:  The URL to convert to absolute

if (substr($page, 0, 7) != "http://") return $url;

$parse = parse_url($page);
$root  = $parse['scheme'] . "://" . $parse['host'];
$p     = strrpos(substr($page, 7), '/');

if ($p) $base = substr($page, 0, $p + 8);
else $base = "$page/";

if (substr($url, 0, 1) == '/')           $url = $root . $url;
elseif (substr($url, 0, 7) != "http://") $url = $base . $url;

return $url;
}

?>


插件说明:

本插件接受一个web也没的URL地址和该页面的一个链接,然后返回这个链接的绝对地址,通过这个地址可以直接访问这个链接页面而无需通过引用页面。简而言之,他返回一个URL绝对地址。本插件需要以下参数:

$page 一个web页面的URL地址,包括”http://"前导符的域名。

$url $page页面上的一个链接。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: