PHP文章采集URL補全函數(shù)(FormatUrl)
更新時間:2012年08月02日 22:46:27 作者:
寫此函數(shù)作用就是為了開發(fā)采集程序,采集文章的時候會經(jīng)常遇到頁面里的路徑是 相對路徑 或者 絕對根路徑 不是 絕對全路徑 就無法收集URL
寫采集必用的函數(shù),URL補全函數(shù),也可叫做FormatUrl。
寫此函數(shù)作用就是為了開發(fā)采集程序,采集文章的時候會經(jīng)常遇到頁面里的路徑是 “相對路徑” 或者 “絕對根路徑” 不是“絕對全路徑”就無法收集URL。
所以,就需要本功能函數(shù)進行對代碼進行格式化,把所有的超鏈接都格式化一遍,這樣就可以直接收集到正確的URL了。
路徑知識普及
相對路徑:“../” “./” 或者前面什么都不加
絕對根路徑:/path/xxx.html
絕對全路徑:http://www.xxx.com/path/xxx.html
使用實例:
<?php
$surl="http://www.dbjr.com.cn/";
$gethtm = '<a href="/index.htm">首頁</a><a href="Resolvent/index.htm">解決方案</a>';
echo formaturl($gethtm,$surl);
?>
輸出:<a href="http://www.dbjr.com.cn/index.htm">首頁</a><a href="http://www.dbjr.com.cn/Resolvent/index.htm">解決方案</a>
--------- 演示實例 ------------
原始路徑代碼:http://www.newnew.cn/newnewindex.aspx
輸出演示代碼:http://www.maifp.com/aaa/test.php
以下是函數(shù)代碼
<?php
function formaturl($l1,$l2){
if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i",$l1,$regs)){
foreach($regs[0] as $num => $url){
$l1 = str_replace($url,lIIIIl($url,$l2),$l1);
}
}
return $l1;
}
function lIIIIl($l1,$l2){
if(preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i",$l1,$regs)){$I2 = $regs[3];}
if(strlen($I2)>0){
$I1 = str_replace(chr(34),"",$I2);
$I1 = str_replace(chr(39),"",$I1);
}else{return $l1;}
$url_parsed = parse_url($l2);
$scheme = $url_parsed["scheme"];if($scheme!=""){$scheme = $scheme."://";}
$host = $url_parsed["host"];
$l3 = $scheme.$host;
if(strlen($l3)==0){return $l1;}
$path = dirname($url_parsed["path"]);if($path[0]=="\\"){$path="";}
$pos = strpos($I1,"#");
if($pos>0) $I1 = substr($I1,0,$pos);
//判斷類型
if(preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+/i",$I1)){return $l1; }//http開頭的url類型要跳過
elseif($I1[0]=="/"){$I1 = $l3.$I1;}//絕對路徑
elseif(substr($I1,0,3)=="../"){//相對路徑
while(substr($I1,0,3)=="../"){
$I1 = substr($I1,strlen($I1)-(strlen($I1)-3),strlen($I1)-3);
if(strlen($path)>0){
$path = dirname($path);
}
}
$I1 = $l3.$path."/".$I1;
}
elseif(substr($I1,0,2)=="./"){
$I1 = $l3.$path.substr($I1,strlen($I1)-(strlen($I1)-1),strlen($I1)-1);
}
elseif(strtolower(substr($I1,0,7))=="mailto:"||strtolower(substr($I1,0,11))=="javascript:"){
return $l1;
}else{
$I1 = $l3.$path."/".$I1;
}
return str_replace($I2,"\"$I1\"",$l1);
}
?>
下面的鏈接是學(xué)習(xí)PHP正則表達式的地方。在這里留個鏈接,防止丟失。。。
寫此函數(shù)作用就是為了開發(fā)采集程序,采集文章的時候會經(jīng)常遇到頁面里的路徑是 “相對路徑” 或者 “絕對根路徑” 不是“絕對全路徑”就無法收集URL。
所以,就需要本功能函數(shù)進行對代碼進行格式化,把所有的超鏈接都格式化一遍,這樣就可以直接收集到正確的URL了。
路徑知識普及
相對路徑:“../” “./” 或者前面什么都不加
絕對根路徑:/path/xxx.html
絕對全路徑:http://www.xxx.com/path/xxx.html
使用實例:
復(fù)制代碼 代碼如下:
<?php
$surl="http://www.dbjr.com.cn/";
$gethtm = '<a href="/index.htm">首頁</a><a href="Resolvent/index.htm">解決方案</a>';
echo formaturl($gethtm,$surl);
?>
輸出:<a href="http://www.dbjr.com.cn/index.htm">首頁</a><a href="http://www.dbjr.com.cn/Resolvent/index.htm">解決方案</a>
--------- 演示實例 ------------
原始路徑代碼:http://www.newnew.cn/newnewindex.aspx
輸出演示代碼:http://www.maifp.com/aaa/test.php
以下是函數(shù)代碼
復(fù)制代碼 代碼如下:
<?php
function formaturl($l1,$l2){
if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i",$l1,$regs)){
foreach($regs[0] as $num => $url){
$l1 = str_replace($url,lIIIIl($url,$l2),$l1);
}
}
return $l1;
}
function lIIIIl($l1,$l2){
if(preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i",$l1,$regs)){$I2 = $regs[3];}
if(strlen($I2)>0){
$I1 = str_replace(chr(34),"",$I2);
$I1 = str_replace(chr(39),"",$I1);
}else{return $l1;}
$url_parsed = parse_url($l2);
$scheme = $url_parsed["scheme"];if($scheme!=""){$scheme = $scheme."://";}
$host = $url_parsed["host"];
$l3 = $scheme.$host;
if(strlen($l3)==0){return $l1;}
$path = dirname($url_parsed["path"]);if($path[0]=="\\"){$path="";}
$pos = strpos($I1,"#");
if($pos>0) $I1 = substr($I1,0,$pos);
//判斷類型
if(preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+/i",$I1)){return $l1; }//http開頭的url類型要跳過
elseif($I1[0]=="/"){$I1 = $l3.$I1;}//絕對路徑
elseif(substr($I1,0,3)=="../"){//相對路徑
while(substr($I1,0,3)=="../"){
$I1 = substr($I1,strlen($I1)-(strlen($I1)-3),strlen($I1)-3);
if(strlen($path)>0){
$path = dirname($path);
}
}
$I1 = $l3.$path."/".$I1;
}
elseif(substr($I1,0,2)=="./"){
$I1 = $l3.$path.substr($I1,strlen($I1)-(strlen($I1)-1),strlen($I1)-1);
}
elseif(strtolower(substr($I1,0,7))=="mailto:"||strtolower(substr($I1,0,11))=="javascript:"){
return $l1;
}else{
$I1 = $l3.$path."/".$I1;
}
return str_replace($I2,"\"$I1\"",$l1);
}
?>
下面的鏈接是學(xué)習(xí)PHP正則表達式的地方。在這里留個鏈接,防止丟失。。。
您可能感興趣的文章:
- PHP自動補全表單的兩種方法
- php截取html字符串及自動補全html標簽的方法
- PHP+jQuery實現(xiàn)自動補全功能源碼
- php使HTML標簽自動補全閉合函數(shù)代碼
- PHP實現(xiàn)HTML標簽自動補全代碼
- Jquery+Ajax+PHP+MySQL實現(xiàn)分類列表管理(下)
- Jquery+Ajax+PHP+MySQL實現(xiàn)分類列表管理(上)
- jQuery+Ajax+PHP+Mysql實現(xiàn)分頁顯示數(shù)據(jù)實例講解
- PHP+jQuery+Ajax+Mysql如何實現(xiàn)發(fā)表心情功能
- php+mysql+jquery實現(xiàn)簡易的檢索自動補全提示功能
相關(guān)文章
PHP執(zhí)行l(wèi)inux命令常用函數(shù)匯總
一般情況下,很少會用php去執(zhí)行l(wèi)inux命令,不過特殊情況下,你也許會用到這些函數(shù)。以前我知道有二個函數(shù)可以執(zhí)行l(wèi)inux命令,一個是exec,一個是shell_exec,通過本文給大家介紹PHP執(zhí)行l(wèi)inux命令常用函數(shù)匯總,需要的朋友參考下2016-02-02Laravel訪問出錯提示:`Warning: require(/vendor/autoload.php): faile
這篇文章主要介紹了Laravel訪問出錯提示:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or di解決方法,涉及Laravel框架相關(guān)配置與安裝操作技巧,需要的朋友可以參考下2019-04-04set_exception_handler函數(shù)在ThinkPHP中的用法
這篇文章主要介紹了set_exception_handler函數(shù)在ThinkPHP中的用法,分析了官方給出了set_exception_handler函數(shù)用法說明及示例,并講述了在ThinkPHP中的應(yīng)用實例,需要的朋友可以參考下2014-10-10