php如何将相对路径转换为绝对路径

来源:百度知道 编辑:UC知道 时间:2024/05/23 20:16:39
http://www.qearth.cn/default.htm中有
这么个链接:newsfile/2008-11-28/20081128160204.html
怎样使之转换为:http://www.qearth.cn/newsfile/2008-11-28/20081128160204.html

有什么好的方法

蜘蛛遇到这种情况是怎样处理的?
http://www.qearth.cn/newsfile/2008-11-28/20081128160204.html

怎样才能取出他所在的目录:例如http://www.qearth.cn/newsfile/2008-11-28/

给你一个函数:
$content:网页内容;
$feed_url:网站域名;

<?
function relative_to_absolute($content, $feed_url) {
preg_match('/(http|https|ftp):\/\//', $feed_url, $protocol);
$server_url = preg_replace("/(http|https|ftp|news):\/\//", "", $feed_url);
$server_url = preg_replace("/\/.*/", "", $server_url);
if ($server_url == '') {
return $content;
}
if (isset($protocol[0])) {
$new_content = preg_replace('/href="\//', 'href="'.$protocol[0].$server_url.'/', $content);
$new_content = preg_replace('/src="\//', 'src="'.$protocol[0].$server_url.'/', $new_content);
} else {
$new_content = $content;
}
return $new_content;
}
?>