Page 1 of 1

Matching a pattern only outside <>tags

Posted: Thu Mar 13, 2008 6:39 pm
by ternto333
I am searching for a pattern not inside <> characters.
For the given string "GKDLSD<span>DLK</span>DGEE", the regex /(s)/i should only return once. What is a regular expression I can use? The above example is very simplified, but that is definitely what I need is a way for the regex to only match characters outside of the html tags

Re: Matching a pattern only outside <>tags

Posted: Sun Apr 06, 2008 4:49 pm
by aCa
Did you want the code in a long string without the html tags or each of the non html text as array?

This isn't that pretty but should do the trick if you wan't a string with the non html code.

Code: Select all

preg_replace('/(<[^>]+>)/', ' ', 'GKDLSD<span>DLK</span>DGEE');
If you wanted it in an array you could use

Code: Select all

preg_replace('/(<[^>]+>)/', '@SPLIT@', 'GKDLSD<span>DLK</span>DGEE');
Then just split on @SPLIT@ and you will have all the non html values in an array.

Test it on http://regex.larsolavtorvik.com/ and see if it works like you wan't.

Re: Matching a pattern only outside <>tags

Posted: Tue Apr 08, 2008 3:14 am
by GeertDD
aCa wrote:Test it on http://regex.larsolavtorvik.com/ and see if it works like you wan't.
Hey, nice tool! Thanks.

Re: Matching a pattern only outside <>tags

Posted: Wed Apr 09, 2008 10:11 am
by prometheuzz
aCa wrote:
...

Test it on http://regex.larsolavtorvik.com/ and see if it works like you wan't.
Well done!