php – 将相对链接转换为绝对链接
发布时间:2020-12-13 17:51:07 所属栏目:PHP教程 来源:网络整理
导读:我正在请求这样的网站的源代码: ? $txt = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741');echo $txt; ? 我想用绝对的替换相关链接!基本上, img src="/images/legend_15s.png"/ and img src='/images/legend_15s.png'/ 应该被替换 img
我正在请求这样的网站的源代码:
<? $txt = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741'); echo $txt; ?> 我想用绝对的替换相关链接!基本上, <img src="/images/legend_15s.png"/> and <img src='/images/legend_15s.png'/> 应该被替换 <img src="http://domain.com/images/legend_15s.png"/> 和 <img src='http://domain.com/images/legend_15s.png'/> 分别.我怎样才能做到这一点?
此代码仅替换链接和图像:
<? $txt = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741'); $txt = str_replace(array('href="','src="'),array('href="http://stats.pingdom.com/','src="http://stats.pingdom.com/'),$txt); echo $txt; ?> 我测试过它的工作:) 更新 这是通过正则表达式完成并更好地工作: <? $txt = file_get_contents('http://stats.pingdom.com/qmwwuwoz2b71/522741'); $domain = "http://stats.pingdom.com"; $txt = preg_replace("/(href|src)="([^(http)])(/)?/","$1="$domain$2",$txt); echo $txt; ?> 完成:D (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |