PHP"preg_replace"向特定域的任何URL添加查询参数 [英] PHP "preg_replace" to add query param to any urls of a certain domain
本文介绍了PHP"preg_replace"向特定域的任何URL添加查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的字符串:
I have a string like:
$description = '
<a href="http://www.replace.com/1st-link/">1st link in string</a>,
<a href="http://www.ignore.com/">2nd link in string</a>, some other text in string,
<a href="http://www.replace.com/3rd-link/">3rd link in string</a>.
';
我需要使用preg_replace来将查询参数?id = awesome"附加到来自"replace.com"的$ description字符串中的所有网址(忽略所有其他链接,即"ignore.com").
I need to use preg_replace to append a query parameter of "?id=awesome" to any urls in the $description string from "replace.com" (ignoring all other links, i.e. "ignore.com").
在此先感谢您的帮助!
推荐答案
好,很简单,在这里:
$content = preg_replace('/http:\/\/[^"]*?\.replace\.com\/[^"]+/','$0?id=awesome',$description);
希望就是这样,$ content将带有字符串,他将参数添加到了replace.com域中:)
Hope that's it, $content will have the string witht he added paramters to the replace.com domain :)
这篇关于PHP"preg_replace"向特定域的任何URL添加查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文