0

我有一个字符串,其中包含 HTML 中的 URL。我想为每个 URL 添加参数。

$my_string = "See our full range of <a href="http://www.mysite.com/cat.php?cPath=8" target="_blank">some category</a> online and also our popular <a href="http://www.mysite.com/product.php?products_id=22" target="_blank">some product</a>.";

最终应该是:

$my_new_string = "See our full range of <a href="http://www.mysite.com/cat.php?cPath=8&new_param=test" target="_blank">some category</a> online and also our popular <a href="http://www.mysite.com/product.php?products_id=22&new_param=test" target="_blank">some product</a>.";
4

1 回答 1

0

尝试一个正则表达式:

$my_new_string = preg_replace('`<a (.*)href="(.+)"(.*)>`iU', '<a $1href="$2&new_param=test"$3>', $my_string);
于 2012-07-23T09:32:19.503 回答