Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regular Expression preg_replace

Ignore text within a href

2:26 pm on Dec 28, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 18, 2003
votes: 0

Trying to automatically insert links in text
cut down example..
$Text = "
<a href='/blue/black/'>black</a>
$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.


10:59 pm on Jan 5, 2006 (gmt 0)


WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
votes: 2

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.
$pattern = "#>([^<]*)$find([^<]*<(?!/a>))#is"; 
$Text = preg_replace($pattern,">$1<a href='someurl'>$find</a>$2",$Text,1);

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.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members