Hi!
I have a situation in which I parse a body of text and replace certain phrases with links. I then need to re-parse the string to replace a second set of phrases with links. The problem arises at this point, where certain words or phrases in the second set can be substrings of phrases already replaced in the first pass.
Example: The string "blah blah grand canyon blah" will become "blah blah <a href="#">grand canyon</a>
blah" after the first pass. The second pass might try to replace the word "canyon" with a link, so the resulting, broken, text would read: "blah blah <a href="#">
grand <a href="#">
canyon</a></a>
blah".
So I've been trying to use preg_replace and a regular expression to prevent nested <a>
tags from occurring - by only replacing text which is not already in a link. I have tried to regexes that check based on whether there are </a>
tags further on in the text but can't get these to work.
Maybe another approach is required?
Many thanks in advance! Dave