Home >Backend Development >PHP Tutorial >Article collection URL completion function (FormatUrl) written in PHP

Article collection URL completion function (FormatUrl) written in PHP

WBOY
WBOYOriginal
2016-07-25 09:05:171208browse
function formaturl($l1,$l2){

if (preg_match_all("/(]+src="([^"]+)"[ ^>]*>)|(]+href="([^"]+)"[^>]*>)|(]+ src='([^']+)'[^>]*>)|(]+href='([^']+)'[^>]*> )/i",$l1,$regs)){foreach($regs[0] as $num => $url){

$l1 = str_replace($url,lIIIIl($url,$l2),$ l1);
}
    }
  1. return $l1;
  2. }
  3. function lIIIIl($l1,$l2){
  4. if(preg_match("/(.*)(href|src)=(.+?)( |/ >|>).*/i",$l1,$regs)){$I2 = $regs[3];}
  5. if(strlen($I2)>0){
  6. $I1 = str_replace(chr (34),"",$I2);
  7. $I1 = str_replace(chr(39),"",$I1);
  8. }else{return $l1;}
  9. $url_parsed = parse_url($l2);
  10. $ scheme = $url_parsed["scheme"];if($scheme!=""){$scheme = $scheme."://";}
  11. $host = $url_parsed["host"];
  12. $l3 = $ scheme.$host;
  13. if(strlen($l3)==0){return $l1;}
  14. $path = dirname($url_parsed["path"]);if($path[0]=="\" ){$path="";}
  15. $pos = strpos($I1,"#");
  16. if($pos>0) $I1 = substr($I1,0,$pos);
  17. //Judge the type
  18. if(preg_match("/^(http|https|ftp):(//|\\)(([w/\+-~`@:%])+.)+([w/\.=? +-~`@':!%#]|(&)|&)+/i",$I1)){return $l1; }//The url type starting with http should be skipped
  19. elseif($I1[0 ]=="/"){$I1 = $l3.$I1;}//Absolute path
  20. elseif(substr($I1,0,3)=="../"){//Relative path
  21. while( substr($I1,0,3)=="../"){
  22. $I1 = substr($I1,strlen($I1)-(strlen($I1)-3),strlen($I1)-3 );
  23. if(strlen($path)>0){
  24. $path = dirname($path);
  25. }
  26. }
  27. $I1 = $l3.$path."/".$I1;
  28. }
  29. elseif (substr($I1,0,2)=="./"){
  30. $I1 = $l3.$path.substr($I1,strlen($I1)-(strlen($I1)-1),strlen ($I1)-1);
  31. }
  32. elseif(strtolower(substr($I1,0,7))=="mailto:"||strtolower(substr($I1,0,11))=="javascript: "){
  33. return $l1;
  34. }else{
  35. $I1 = $l3.$path."/".$I1;
  36. }
  37. return str_replace($I2,""$I1"",$l1);
  38. }
  39. ?>
  40. Copy code
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