Ok, so I have a string (it's the contents of an email), and I need to append a variable to any URL that is present on that string. we can consider that all URLs are inside of href attributes of anchor tags So, I want to search for any occurence of href="whatever" and replace it for href="whatever?myvar". Ideally, I would also like to check if the link already has any var in it, in order to append it with "&myvar" instead of "?myvar".
I have something like this, but I get lost with regex expressions..
$pattern = '"\b(http?://\S+)"';
$html_links = preg_replace($pattern, '$1&myvar', $text);
this ain't working because it is appending my var AFTER the closing double quote for the href attribute...
Sorry, I'm so bad with regex. Any help will be highly appreciated!