Matching a pattern only outside <>tags

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ternto333
Forum Newbie
Posts: 1
Joined: Thu Mar 13, 2008 6:35 pm

Matching a pattern only outside <>tags

Post 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
aCa
Forum Newbie
Posts: 11
Joined: Sun Apr 06, 2008 3:43 pm

Re: Matching a pattern only outside <>tags

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Matching a pattern only outside <>tags

Post by GeertDD »

aCa wrote:Test it on http://regex.larsolavtorvik.com/ and see if it works like you wan't.
Hey, nice tool! Thanks.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Matching a pattern only outside <>tags

Post by prometheuzz »

aCa wrote:
...

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