Page 1 of 1

Negating...

Posted: Wed Oct 26, 2005 1:48 pm
by Ree
I want to match all tags which are not indicated tags. How do I do this? Say, the start of a tag:

Code: Select all

$pattern = '/<td.*?>/'is;
How do I match ANY starting tag which is not a td starting tag?

Posted: Wed Oct 26, 2005 1:56 pm
by Chris Corbyn

Code: Select all

$re = '/<(?!td)[^>]*>/is';
[^chars] is used to negate a character class - the characters dont have to come in the order you put them in the brackets though

(?!pattern) is a negative lookahead.

<(?!td) means any "<" that's NOT followed by td

;)

PS: Don't forgret to put the pattern in quotes in PHP, unlike perl and javascript :)

Posted: Wed Oct 26, 2005 2:02 pm
by Ree
Thanks, will try that. :)

Posted: Wed Oct 26, 2005 2:10 pm
by Ree
And if I wanted to add some more tags besides td, say, tr, would it look like this?

Code: Select all

$pattern = '/<(?!(td|tr))[^>]*>/is';

Posted: Wed Oct 26, 2005 3:22 pm
by Ree
I have tried this to remove any tags in the string that are not td or tr:

Code: Select all

$table = preg_replace('/<[\/]?(?!td|tr).*?>/is', '', $table);
For some reason it also deletes ending td and tr tags (</td>, </tr>). What is wrong with my regex?

Posted: Wed Oct 26, 2005 5:30 pm
by Chris Corbyn
That's a very good question...and one that after much (drunken) consideration I came to this conclusion...

<[\/]? That'll match the T or the D, and so TD does not follow either....


;)

Posted: Thu Oct 27, 2005 12:59 am
by Ree
I did this and it works well now:

Code: Select all

$table = preg_replace('/<(?![\/]?(td|tr)).*?>/is', '', $table);

Posted: Thu Oct 27, 2005 5:48 am
by Chris Corbyn
Ree wrote:I did this and it works well now:

Code: Select all

$table = preg_replace('/<(?![\/]?(td|tr)).*?>/is', '', $table);
Very clever ;)