Regular Expressions, avoiding HTML tags in PHP

Posted by Jason Axelrod on Stack Overflow See other posts from Stack Overflow or by Jason Axelrod
Published on 2011-01-11T14:52:00Z Indexed on 2011/01/11 15:53 UTC
Read the original article Hit count: 151

Filed under:
|
|

I have actually seen this question quite a bit here, but none of them are exactly what I want... Lets say I have the following phrase:

Line 1 - This is a TEST phrase.
Line 2 - This is a <img src="TEST" /> image.
Line 3 - This is a <a href="somelink/TEST">TEST</a> link.

Okay, simple right? I am trying the following code:

$linkPin = '#(\b)TEST(\b)(?![^<]*>)#i';
$linkRpl = '$1<a href="newurl">TEST</a>$2';

$html = preg_replace($linkPin, $linkRpl, $html);

As you can see, it takes the word TEST, and replaces it with a link to test. The regular expression I am using right now works good to avoid replacing the TEST in line 2, it also avoids replacing the TEST in the href of line 3. However, it still replaces the text encapsulated within the tag on line 3 and I end up with:

Line 1 - This is a <a href="newurl">TEST</a> phrase.
Line 2 - This is a <img src="TEST" /> image.
Line 3 - This is a <a href="somelink/TEST"><a href="newurl">TEST</a></a> link.

This I do not want as it creates bad code in line 3. I want to not only ignore matches inside of a tag, but also encapsulated by them. (remember to keep note of the /> in line 2)

© Stack Overflow or respective owner

Related posts about php

Related posts about html