Php preg_替换href值中的更改链接,但不在src中
我需要替换curl获取的页面中的URL,并添加正确的链接。我的php curl代码是:Php preg_替换href值中的更改链接,但不在src中,php,regex,preg-replace,preg-match,preg-match-all,Php,Regex,Preg Replace,Preg Match,Preg Match All,我需要替换curl获取的页面中的URL,并添加正确的链接。我的php curl代码是: <?php $string = '<a href="http://host.org"><img src="./sec.png"></a>'; $string = preg_replace('/href="(http:\/\/([^\/]+)\/)?([^"]+)"/', "href=\"http://google.com/\\3\"", $string); ec
<?php
$string = '<a href="http://host.org"><img src="./sec.png"></a>';
$string = preg_replace('/href="(http:\/\/([^\/]+)\/)?([^"]+)"/', "href=\"http://google.com/\\3\"", $string);
echo $string;
?>
从regexp中删除此不必要的部分:([^/]+)/
以下预替换应该可以工作:
preg_replace('/href="(http:\/\/[^\/"]+\/?)?([^"]*)"/', "href=\"http://google.com/\\2\"", $result);
可能是重复的哦,不,没有重复,第一个是preg_match,以查找是否有http链接,我已经剪切了它,使问题更简单。谢谢
preg_replace('/href="(http:\/\/[^\/"]+\/?)?([^"]*)"/', "href=\"http://google.com/\\2\"", $result);