matching tags not commented out
Moderator: General Moderators
matching tags not commented out
I've been struggling,
How do I match:
<element>Something</element>
but not:
<!-- <element>Something</element> -->
for use with preg_replace.
Thanks in advance.
How do I match:
<element>Something</element>
but not:
<!-- <element>Something</element> -->
for use with preg_replace.
Thanks in advance.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: matching tags not commented out
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
wow dude. works great - thanks.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: matching tags not commented out
No problem.Glycerine wrote:wow dude. works great - thanks.
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
I just about do - Its far more advanced to what I'm capable. I had this:
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.
Code: Select all
$result = preg_replace('%^<element[^>]*>(.*?)</element>|-->^%simx', '$1 ', $subject);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.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: matching tags not commented out
With a preg_replace_all(...), you mean? Which information exactly?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.
Re: matching tags not commented out
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.
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.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: matching tags not commented out
I see. Sure, just wrap parentheses around this part of my regex: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.
Code: Select all
[^<]*Re: matching tags not commented out
Got it just as you posted it.
Perfect.
Perfect.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: matching tags not commented out
Good to hear it!Glycerine wrote:Got it just as you posted it.
Perfect.