matching tags not commented out

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

Moderator: General Moderators

Post Reply
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

matching tags not commented out

Post by Glycerine »

I've been struggling,

How do I match:
<element>Something</element>

but not:

<!-- <element>Something</element> -->

for use with preg_replace.

Thanks in advance.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: matching tags not commented out

Post by prometheuzz »

Code: Select all

$text = '<element1>Something1</element1> 
<!-- <element2>Something2</element2> --> 
<element3>Something3</element3>';
$regex = '#<([^>]*)>[^<]*</\1>(?!(?:(?!<--)[^<>-])*-->)#';
echo $text . "\n----------------------------------------";
echo preg_replace($regex, '', $text);
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: matching tags not commented out

Post by Glycerine »

wow dude. works great - thanks.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: matching tags not commented out

Post by prometheuzz »

Glycerine wrote:wow dude. works great - thanks.
No problem.
Since you didn't ask, I presume you understand the regex I psoted? ; )
If not, and you'd like a (short) explanation: just ask.

Good luck.
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: matching tags not commented out

Post by Glycerine »

I just about do - Its far more advanced to what I'm capable. I had this:

Code: Select all

$result = preg_replace('%^<element[^>]*>(.*?)</element>|-->^%simx', '$1 ', $subject);
but it would only work with the start comment out not the end one-

If I can ask - is there a way I can collect the information within the tags? If its too much hassle - don't fuss.

Thanks again.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: matching tags not commented out

Post by prometheuzz »

Glycerine wrote:...

If I can ask - is there a way I can collect the information within the tags? If its too much hassle - don't fuss.

Thanks again.
With a preg_replace_all(...), you mean? Which information exactly?
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: matching tags not commented out

Post by Glycerine »

OOH - sorry,

In your regex you have the ability to referance the tag name with backreferance $1 - Can I collect the information within the tags as back referance $2

<element>something</element>

$1 = element
$2 = something

Hopefully I've made my jumble understandable.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: matching tags not commented out

Post by prometheuzz »

Glycerine wrote:OOH - sorry,

In your regex you have the ability to referance the tag name with backreferance $1 - Can I collect the information within the tags as back referance $2

<element>something</element>

$1 = element
$2 = something

Hopefully I've made my jumble understandable.
I see. Sure, just wrap parentheses around this part of my regex:

Code: Select all

[^<]*
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: matching tags not commented out

Post by Glycerine »

Got it just as you posted it.

Perfect.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: matching tags not commented out

Post by prometheuzz »

Glycerine wrote:Got it just as you posted it.

Perfect.
Good to hear it!
Post Reply