循环遍历字符串中的url并将值附加到PHP正则表达式

时间:2010-07-19 09:03:41

标签: php regex

我有以下字符串例如:

Welcome to my <a href="http://example.com">site</a>. Feel free to  <a href="http://localhost.com">contact me</a>.

我可以使用什么技术来评估此字符串,以便在两个网址的末尾添加“/ id / 123”:

Welcome to my <a href="http://example.com/id/123">site</a>. Feel free to  <a href="http://localhost.com/id/123">contact me</a>.

感谢。

2 个答案:

答案 0 :(得分:2)

这应该有效:

$string = preg_replace('/href="([^"]*)"/','href="\\1/id/123"',$string);

答案 1 :(得分:0)

$result = preg_replace('/<a\s+href="([^"]+)"/', '<a href="\1/id/123"', $subject);