Page 1 of 1
matching tags not commented out
Posted: Sat Jan 03, 2009 8:39 am
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.
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 9:00 am
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);
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 9:12 am
by Glycerine
wow dude. works great - thanks.
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 9:20 am
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.
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 9:31 am
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.
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 9:43 am
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?
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 9:46 am
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.
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 10:38 am
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:
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 10:44 am
by Glycerine
Got it just as you posted it.
Perfect.
Re: matching tags not commented out
Posted: Sat Jan 03, 2009 11:09 am
by prometheuzz
Glycerine wrote:Got it just as you posted it.
Perfect.
Good to hear it!