我有以下字符串例如:
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>.
感谢。
答案 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);