[英]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.