Msg#: 11173 posted 2:26 pm on Dec 28, 2005 (gmt 0)
Trying to automatically insert links in text cut down example.. <?PHP $Text = " blue! <a href='/blue/black/'>black</a> yellow"; $find="black"; $Text = @preg_replace("#\b($find)\b#is","<a href='someurl'>$1</a>",$Text,1); ?> However, the code above will match text which is already in links.
Is there a way to replace text and have the expression ignore text in between hrefs - stripping html is not a solution unless you can put it back again, need to keep existing html intact.
Msg#: 11173 posted 10:59 pm on Jan 5, 2006 (gmt 0)
Here is something that may work, if the existing text is truly a full html text source (assumption based on there always being a beginning element and an ending element, if not, you would need to add a '>' to the beginning of the $string you are searching as well as an '>' to the end.
I think I have that right ;-) The pattern says to find anything in the string that begins with a closing element tag '>' followed by zero or more of anything that is not an opening element tag '([^<]*)' -- and capture that part, followed by our search term, optionally followed by anything that is not an opening element tag followed by an opening element tag which is not the beginning of a closing <a> element '([^<]*<(?!/a>))' -- capturing this portion as well.
The captured parts get used in the replacement value as variables $1 and $2.
The biggest trick going on here is that we are using a negative lookahead assertion. You can read more about these fun tricks in the PHP PCRE Pattern Syntax [php.net] manual pages.