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 に答える