【问题标题】:preg_replace change link in href value, but not in srcpreg_replace 在 href 值中更改链接,但不在 src 中
【发布时间】:2013-10-04 21:20:25
【问题描述】:

我需要替换 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);

echo $string;

?>

当链接为“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>

任何机构都可以帮助解决它。

【问题讨论】:

  • 哦,不,没有重复,第一个是 preg_match 以查找是否有 http 链接,我已将其剪切,以使问题更简单。谢谢。

标签: php regex preg-replace preg-match preg-match-all


【解决方案1】:

以下 preg_replace 应该可以工作:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多