当前位置:Gxlcms > PHP教程 > php写的文章采集URL补全函数(FormatUrl)

php写的文章采集URL补全函数(FormatUrl)

时间:2021-07-01 10:21:17 帮助过:17人阅读

  1. $surl="http://bbs.it-home.org/";
  2. $gethtm = '首页解决方案';
  3. echo formaturl($gethtm,$surl);
  4. ?>

输出:首页解决方案 --------- 演示实例 ------------ 原始路径代码:http://www.newnew.cn/newnewindex.aspx 输出演示代码:http://www.maifp.com/aaa/test.php 函数代码:

  1. function formaturl($l1,$l2){
  2. if (preg_match_all("/(]+src=\"([^\"]+)\"[^>]*>)|(]+href=\"([^\"]+)\"[^>]*>)|(]+src='([^']+)'[^>]*>)|(]+href='([^']+)'[^>]*>)/i",$l1,$regs)){
  3. foreach($regs[0] as $num => $url){
  4. $l1 = str_replace($url,lIIIIl($url,$l2),$l1);
  5. }
  6. }
  7. return $l1;
  8. }
  9. function lIIIIl($l1,$l2){
  10. if(preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i",$l1,$regs)){$I2 = $regs[3];}
  11. if(strlen($I2)>0){
  12. $I1 = str_replace(chr(34),"",$I2);
  13. $I1 = str_replace(chr(39),"",$I1);
  14. }else{return $l1;}
  15. $url_parsed = parse_url($l2);
  16. $scheme = $url_parsed["scheme"];if($scheme!=""){$scheme = $scheme."://";}
  17. $host = $url_parsed["host"];
  18. $l3 = $scheme.$host;
  19. if(strlen($l3)==0){return $l1;}
  20. $path = dirname($url_parsed["path"]);if($path[0]=="\\"){$path="";}
  21. $pos = strpos($I1,"#");
  22. if($pos>0) $I1 = substr($I1,0,$pos);
  23. //判断类型
  24. if(preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+/i",$I1)){return $l1; }//http开头的url类型要跳过
  25. elseif($I1[0]=="/"){$I1 = $l3.$I1;}//绝对路径
  26. elseif(substr($I1,0,3)=="../"){//相对路径
  27. while(substr($I1,0,3)=="../"){
  28. $I1 = substr($I1,strlen($I1)-(strlen($I1)-3),strlen($I1)-3);
  29. if(strlen($path)>0){
  30. $path = dirname($path);
  31. }
  32. }
  33. $I1 = $l3.$path."/".$I1;
  34. }
  35. elseif(substr($I1,0,2)=="./"){
  36. $I1 = $l3.$path.substr($I1,strlen($I1)-(strlen($I1)-1),strlen($I1)-1);
  37. }
  38. elseif(strtolower(substr($I1,0,7))=="mailto:"||strtolower(substr($I1,0,11))=="javascript:"){
  39. return $l1;
  40. }else{
  41. $I1 = $l3.$path."/".$I1;
  42. }
  43. return str_replace($I2,"\"$I1\"",$l1);
  44. }
  45. ?>

人气教程排行