Forum Moderators: coopster
I have a webblog, which is simple text. However, I include a lot of links to Amazon products, for more information on stuff I mention.
I would like to use some sort of RegEx with my PHP to change Amazon links to include my Amazon Associate id.
So:
$mytext = '<p>This is <A HREF="http://www.amazon.com/exec/obidos/ASIN/0679781587">link</A> bla bla bla bla bla bla bla.<br>This is a <a href="http://www.google.com">normal link</a>, and should not be changed. And here is another, bla bla bla bla bla bla bla<a href="http://www.amazon.com/exec/obidos/ASIN/0316763721">2nd link</a>. Bla bla bla bla.</p>';
would thus become:
$mytext = '<p>This is <A HREF="http://www.amazon.com/exec/obidos/ASIN/0679781587/MY-AMAZON-ID">link</A> bla bla bla bla bla bla bla.<br>This is a <a href="http://www.google.com">normal link</a>, and should not be changed. And here is another, bla bla bla bla bla bla bla<a href="http://www.amazon.com/exec/obidos/ASIN/0316763721/MY-AMAZON-ID">2nd link</a>. Bla bla bla bla.</p>';
However, I have tried a number of things, but am not able to get this done. Hope someone out there can help.
Cheers!
Regards
Mirthe
$str = '<a href="whatever/1234567890">link<a> <a href="yada">leave this alone</a><br>';
$str2 = '<a href="whatever/1234567890/">other link</a> <a href="yada">leave this alone</a>';
print ereg_replace('([0-9]{10})/?"', '\\1/myid/"', $str);
print ereg_replace('([0-9]{10})/?"', '\\1/myid/"', $str2);
the /? should catch optional trailing slashes