I’m wondering what the regex for an eregi_replace would be needed to catch a string that is not contained in an alt attribute.
e.g. It should find and replace John Doe in:
“John Doe was born on…”
but not find/replace when John Doe’s in any tag for example:
<img src="/jd.jpg" alt="John Doe at the beach" />
If I wanted to replace “John Doe” if it’s not inside a tag, I would do this:
(?![^<>]*+>)is a negative lookahead; it says “if there are any angle brackets ahead of this point, the first one is not a closing bracket.” That’s not foolproof, since attribute values can contain angle brackets, but in my experience they rarely do.Regexes are fundamentally incompatible with HTML; even with the advanced features offered by the
preg_suite, like lookarounds and possessive quantifiers, you often have to rely on simplifying assumptions like no angle brackets in attribute values. I wouldn’t even attempt this job with the much-more-limitedereg_functions.