Reputation: 90
I'd like to run a preg_replace
PHP function on a string but I need it to ignore anything inside anchor tags.
eg.
string = 'alpha beta delta gamma <a href="somelink.html">alpha beta delta gamma</a>'
to match = 'beta delta'
How would I get it to only pick up on the first instance of beta delta
and not the second?
Upvotes: 0
Views: 830
Reputation: 54984
You can negative lookahead for a closing tag:
preg_replace("/$to_match(?![^<]*<\/)/", 'foo', $string);
Also you shouldn't use regex on html.
Upvotes: 1