日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

PHP文章采集URL補全函數(shù)_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:PHP中全局變量global和$GLOBALS[]的區(qū)別
一、舉例比較 例一: ?php $var1 =1; function test(){ unset( $GLOBALS [ 'var1' ]); } test(); echo $var1 ; ? 因為$var1被刪除了,所以什么東西都沒有打

寫采集必用的函數(shù),URL補全函數(shù),也可叫做FormatUrl。

寫此函數(shù)作用就是為了開發(fā)采集程序,采集文章的時候會經(jīng)常遇到頁面里的路徑是 “相對路徑” 或者 “絕對根路徑” 不是“絕對全路徑”就無法收集URL。

所以,就需要本功能函數(shù)進行對代碼進行格式化,把所有的超鏈接都格式化一遍,這樣就可以直接收集到正確的URL了。

路徑知識普及

相對路徑:“../” “./” 或者前面什么都不加

絕對根路徑:/path/xxx.html

絕對全路徑:http://www.xxx.com/path/xxx.html

使用實例:

  1. <?php 
  2. $surl="http://www.soqi.cc/"
  3. $gethtm = '<a href="/">首頁</a><a href="/daxue/">大學(xué)排行</a>'
  4. echo formaturl($gethtm,$surl); 
  5. ?> 

輸出:

  1. <a href=http://www.soqi.cc>首頁</a><a href=http://www.soqi.cc/daxue/>大學(xué)排行</a> 

函數(shù)代碼如下:

  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 ( $I1strlen ( $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 ( $I1strlen ( $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. ?>   

分享:php輸出echo、print、print_r、printf、sprintf、var_dump比較
一、echo echo() 實際上不是一個函數(shù),是php語句,因此您無需對其使用括號。不過,如果您希望向 echo() 傳遞一個以上的參數(shù),那么使用括號會發(fā)生解析錯誤。而且echo是返回void的,并不返回值,所以不能使用它來賦值。 例子: 1 ? php 2 $a = echo (55nav); // 錯誤!不

來源:未知//所屬分類:PHP教程/更新時間:2012-08-05
相關(guān)PHP教程