Home >php教程 >php手册 >php把相对路径转换成绝对路径方法

php把相对路径转换成绝对路径方法

WBOY
WBOYOriginal
2016-05-25 16:46:011092browse

我们经常会碰到采集内容时,要补全地址是吧,下面我们来看看这款php把相对路径转换成绝对路径方法吧,相对路径转化成绝对路径实例代码如下:

<?php
function relative_to_absolute($content, $feed_url) {  
    preg_match(&#39;/(http|https|ftp):///&#39;, $feed_url, $protocol);  
    $server_url = preg_replace("/(http|https|ftp|news):///", "", $feed_url);  
    $server_url = preg_replace("//.*/", "", $server_url); 
 
    if ($server_url == &#39;&#39;) {  
        return $content;  
    } 
 
    if (isset($protocol[0])) {  
        $new_content = preg_replace(&#39;/href="//&#39;, &#39;href="&#39;.$protocol[0].$server_url.&#39;/&#39;, $content);  
        $new_content = preg_replace(&#39;/src="//&#39;, &#39;src="&#39;.$protocol[0].$server_url.&#39;/&#39;, $new_content);  
    } else {  
        $new_content = $content;  
    }  
    return $new_content;  
} 
//取得所有链接 
function get_all_url($code){  
    preg_match_all(&#39;/<a\s+href=[\"|\&#39;]?([^>\"\&#39; ]+)[\"|\&#39;]?\s*[^>]*>([^>]+)<\/a>/i&#39;,$code,$arr);  
    return array(&#39;name&#39;=>$arr[2],&#39;url&#39;=>$arr[1]);  
}


本文链接:

收藏随意^^请保留教程地址.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn