简体   繁体   English

preg_replace更改href值中的链接,但不在src中

[英]preg_replace change link in href value, but not in src

I need to replace urls in the page taken by curl and add correct link. 我需要更换curl页面中的url并添加正确的链接。 My php curl code is: 我的php卷曲代码是:

<?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;

?>

When the link is "a" it cut all link and left only href value. 当链接为“a”时,它会切断所有链接并仅保留href值。

//from
<a href="http://host.org"><img src="./sec.png"></a>

//to BUGgg when href fix make :
<a href="http://google.com/./sec.png"></a>

Can any body help in fix it. 任何身体都可以帮助解决它。

以下preg_replace应该工作:

preg_replace('/href="(http:\/\/[^\/"]+\/?)?([^"]*)"/', "href=\"http://google.com/\\2\"", $result);

从正则表达式中删除这个不必要的部分: ([^/]+)/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM