PHP文章采集URL补全函数

PHP文章采集URL补全函数

作者:elinstudio 发布时间:August 1, 2012 分类:PHP      原文地址:http://blog.micxp.com/index.php/archives/91/

写采集必用的函数,URL补全函数,也可叫做FormatUrl。

写此函数作用就是为了开发采集程序,采集文章的时候会经常遇到页面里的路径是 “相对路径” 或者 “绝对根路径” 不是“绝对全路径”就无法收集URL。

所以,就需要本功能函数进行对代码进行格式化,把所有的超链接都格式化一遍,这样就可以直接收集到正确的URL了。

路径知识普及
相对路径:“../” “./” 或者前面什么都不加
绝对根路径:/path/xxx.html
绝对全路径:http://www.xxx.com/path/xxx.html

使用实例:

<?php
$surl="http://www.soqi.cc/";
$gethtm = '<a href="/">首页</a><a href="/daxue/">大学排行</a>';
echo formaturl($gethtm,$surl);
?>

输出:<a href=http://www.soqi.cc>首页</a><a href=http://www.soqi.cc/daxue/>大学排行</a>

函数代码如下:

    1. <?php   
    2.  function formaturl($l1$l2) {   
    3.     if (preg_match_all ( "/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i"$l1$regs )) {   
    4.         foreach ( $regs [0] as $num => $url ) {   
    5.             $l1 = str_replace ( $url, lIIIIl ( $url$l2 ), $l1 );   
    6.         }   
    7.     }   
    8.     return $l1;   
    9. }   
    10.  function lIIIIl($l1$l2) {   
    11.     if (preg_match ( "/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i"$l1$regs )) {   
    12.         $I2 = $regs [3];   
    13.     }   
    14.     if (strlen ( $I2 ) > 0) {   
    15.         $I1 = str_replace ( chr ( 34 ), ""$I2 );   
    16.         $I1 = str_replace ( chr ( 39 ), ""$I1 );   
    17.     } else {   
    18.         return $l1;   
    19.     }   
    20.     $url_parsed = parse_url ( $l2 );   
    21.     $scheme = $url_parsed ["scheme"];   
    22.     if ($scheme != "") {   
    23.         $scheme = $scheme . "://";   
    24.     }   
    25.     $host = $url_parsed ["host"];   
    26.     $l3 = $scheme . $host;   
    27.     if (strlen ( $l3 ) == 0) {   
    28.         return $l1;   
    29.     }   
    30.     $path = dirname ( $url_parsed ["path"] );   
    31.     if ($path [0] == "\\") { 
    32.         $path = ""; 
    33.     } 
    34.     $pos = strpos ( $I1, "#" ); 
    35.     if ($pos > 0) 
    36.         $I1 = substr ( $I1, 0, $pos ); 
    37.      
    38.         //判断类型 
    39.     if (preg_match ( "/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&amp;)|&)+/i", $I1 )) { 
    40.         return $l1; 
    41.     } //http开头的url类型要跳过 
    42. elseif ($I1 [0] == "/") { 
    43.         $I1 = $l3 . $I1; 
    44.     } //绝对路径 
    45. elseif (substr ( $I1, 0, 3 ) == "../") { //相对路径 
    46.         while ( substr ( $I1, 0, 3 ) == "../" ) { 
    47.             $I1 = substr ( $I1, strlen ( $I1 ) - (strlen ( $I1 ) - 3), strlen ( $I1 ) - 3 ); 
    48.             if (strlen ( $path ) > 0) { 
    49.                 $path = dirname ( $path ); 
    50.             } 
    51.         } 
    52.         $I1 = $l3 . $path . "/" . $I1; 
    53.     } elseif (substr ( $I1, 0, 2 ) == "./") { 
    54.         $I1 = $l3 . $path . substr ( $I1, strlen ( $I1 ) - (strlen ( $I1 ) - 1), strlen ( $I1 ) - 1 ); 
    55.     } elseif (strtolower ( substr ( $I1, 0, 7 ) ) == "mailto:" || strtolower ( substr ( $I1, 0, 11 ) ) == "javascript:") { 
    56.         return $l1; 
    57.     } else { 
    58.         $I1 = $l3 . $path . "/" . $I1; 
    59.     } 
    60.     return str_replace ( $I2, "\"$I1\""$l1 );   
    61. }   
    62. ?>  
posted @ 2012-08-02 14:35  潇湘双雁  阅读(1097)  评论(0)    收藏  举报