I\'m trying to replace all link href\'s in a large string with a different URL. With the following code It seems to replace only the 2nd link leaving the first one intact, c
Just use the greedy operator in your regex like this:
'/<a(.*?)href="(.*?)"(.*?)>/'
Slight modifications to Aurelio De Rosa's answer:
'/<a(.*?)href=(["\'])(.*?)\\2(.*?)>/i'
Instead of any char .
use any not (^) quote [^"]
echo preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a$1href="javascript:alert(\'Test\');"$3>',$string_of_text);