user1494917
user1494917

Reputation: 90

regex for content not contained within anchor tags

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

Answers (1)

pguardiario
pguardiario

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

Related Questions