开发者

Cycling through urls in a string and appending a value to it PHP regex

开发者 https://www.devze.com 2023-01-07 16:45 出处:网络
I have the following string for example: Welcome to my <a href=\"http://example.com\">site</a>. Feel free to<a href=\"http://localhost.com\">contact me</a>.

I have the following string for example:

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

What te开发者_StackOverflow社区chnique may I use to evaluate this string to add '/id/123' at the end of both of the urls resulting in:

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>.

Thanks.


This should work:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消