Header redirect with multiple get variables not working

Hi,
i am setting up our enewsletter system so that when we send it it automatically searches through the text and changes the link to a different one with an idenifier and logs it in a db table. I can then point all links to a page which records that the link has been used and redirects them to the original link from the table.

I’ve got the header redirect working fine but i need to beable to change the url. I tried preg_replace to just add in some text before the address and just make the original address a &_get variable but this falls down if the link contains a ‘&’ eg www.something.com?link=http://www.another.com?rel=1&rel2=2
the rel2 is seen as a separate variable and not part of link, which i guess makes sense as it hits the & and thinks its different.

so now i’m thinking of changing all of the address for another using something like this


<?php 
$data1 = "This is one website <a href=\\"example.com\\">blah</a> oh but there is also this one <a href=\\"example2.com\\">blah</a>";
preg_match('/\\="(.*?)\\">/', $data1, $matches);

// here we would insert the url into the database and generate a random number for the url to replace it with.
$domain = $matches[1];

//just echo out the link for testing
echo $domain;

?>

Only problem is it only finds the first url in the string. How do i make it change for everyone it finds.
any help is much appreciated. thanks

The preg_match_all() function is the right function to use in such case.