Php不使用正则实现相对链接转绝对链接

调用方法:echo absolutepath(网页源码,网址);

PHP代码
  1. <?php  
  2. function absolutepath($html,$url){  
  3. //$url=current_url();  
  4. $url=strtolower($url);  
  5. $url=str_replace('http://','',$url);  
  6. $str=explode('/',$url);  
  7. $count=count($str);  
  8. $domain=$str[0];  
  9. for($i=1;$i<$count;$i++){  
  10.     $k=$count-$i;  
  11.     $path='';  
  12.     $absolutepath='';  
  13.     for($j=1;$j<$i;$j++){  
  14.         $absolutepath=$absolutepath.$str[$j].'/';  
  15.     }  
  16.     for($j=1;$j<$k;$j++){  
  17.         $path=$path.'../';  
  18.     }  
  19.     $html=str_replace('href="'.$path,'href="http://'.$domain.'/'.$absolutepath,$html);  
  20.     $html=str_replace('src="'.$path,'src="http://'.$domain.'/'.$absolutepath,$html);  
  21. }  
  22. $str=explode('href="',$html);  
  23. $count=count($str);  
  24. for($i=1;$i<$count;$i++){  
  25.     $str2=explode('"',$str[$i]);  
  26.     $url=$str2[0];  
  27.     if(strpos($url,'http://')>=0){  
  28.         $html=str_replace('href="'.$url,'href="'.$url,$html);  
  29.     }  
  30.     else{  
  31.         if(substr($url,0,1)=='/'){  
  32.             $url='http://'.$domain.$url;  
  33.         }  
  34.         else{  
  35.             $url='http://'.$domain.'/'.$url;  
  36.         }  
  37.         $html=str_replace('href="'.$url,'href="'.$url,$html);  
  38.     }  
  39. }  
  40. $str=explode('src="',$html);  
  41. $count=count($str);  
  42. for($i=1;$i<$count;$i++){  
  43.     $str2=explode('"',$str[$i]);  
  44.     $url=$str2[0];  
  45.     if(strpos($url,'http://')>=0){  
  46.         $html=str_replace('src="'.$url,'src="'.$url,$html);  
  47.     }  
  48.     else{  
  49.         if(substr($url,0,1)=='/'){  
  50.             $url='http://'.$domain.$url;  
  51.         }  
  52.         else{  
  53.             $url='http://'.$domain.'/'.$url;  
  54.         }  
  55.         $html=str_replace('src="'.$url,'src="'.$url,$html);  
  56.     }  
  57. }  
  58. return $html;  
  59. }  
  60. ?>  

原创代码,转载请注明出处www.tianzhigang.com



上一篇: Php获取当前网址URL
下一篇: Javascript分割字符
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: php
相关日志:
评论: 1 | 引用: 0 | 查看次数: 3080
龙岩肛肠医院[2012-06-15 03:23 PM | 访问 http://www.lyzygc.com | Mail To:bbs@163.com | 218.6.36.115 | del | 回复回复]
沙发
对这个概念还不是非常懂啊呵呵
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑