簡體   English   中英

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

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

我需要更換curl頁面中的url並添加正確的鏈接。 我的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;

?>

當鏈接為“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_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