I wrote this script to do this but there is an issue I couldn't figure out:
$buffer = '<a href="http://wwww.domain.com">Domain1</a>';
$buffer .= '<a href="http://wwww.domain.com?id=2">Domain2</a>';
preg_match_all('/<a href="(.*?)"/s', $buffer, $matches);
$searches = array();
$replaces = array();
foreach($matches[1] as $link){
$contain = parse_url($link, PHP_URL_QUERY);
$symbol = $contain ? "&" : "?";
$new_link = $link . $symbol . "mode=testing";
$searches[] = $link;
$replaces[] = $new_link;
}
$newbuffer = str_replace($searches ,$replaces , $buffer);
var_dump($newbuffer);
Output:
<a href="http://wwww.domain.com?mode=testing">Domain1</a>
<a href="http://wwww.domain.com?mode=testing?id=2">Domain2</a>
Expected Output thats adding the parameter to each link:
<a href="http://wwww.domain.com?mode=testing">Domain1</a>
<a href="http://wwww.domain.com?id=2&mode=testing">Domain2</a>
Any help?