Nested Tag Replacement...????

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BuzzCut
Forum Newbie
Posts: 2
Joined: Sat Aug 05, 2006 9:59 am

Nested Tag Replacement...????

Post by BuzzCut »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


OK if you are a PHP Guru you may be able to help me out here...
I have to replace some text in a script and here is what I am trying to accomplish:::

This will replace anything in between START and END.. note the (.*?)

Code: Select all

$FFETADQW = preg_replace('/START.*?END/smi', '', $FFETADQW);
So you can replace anything in a particluar DIV TAG.. (DIV=MYTAG)

Code: Select all

$FFETADQW = preg_replace('/<div id="MYTAG".*?<\/div>/smi', '', $FFETADQW);
Now here is my problem...
The tag I want to replace has several NESTED div tags inside it...
(Example)

Code: Select all

<div id="MYTAG">
Main Content...
<div id="NESTEDTAG1">Content
<div id="NESTEDTAG2">Content
</div>
</div>
</div>
So if I want to replace the "MYTAG" in that example with:::

Code: Select all

$FFETADQW = preg_replace('/<div id="MYTAG".*?<\/div>/smi', '', $FFETADQW);
It will replace all the text in red below::::

Code: Select all

<div id="MYTAG">
Main Content...
<div id="NESTEDTAG1">Content
<div id="NESTEDTAG2">Content
</div>
</div>
</div>
Leaving me with a messed up page... seeing as it leaves two instances of "</div>" that do not belong.....

Note: Things I Have Tried:::
I have tried replacing the div tags from the inside out..
Replacing the tags inside the main tag I want to replace first
First Replacing -- <div id="NESTEDTAG2">Content</div>
Second Replacing -- <div id="NESTEDTAG1">Content</div>
Third Replacing -- <div id="MYTAG">Content</div>
Doesn't work like that... The Nested Tags do not always have the same ID or CLASS....

I have tried:

Code: Select all

$FFETADQW = preg_replace('/<div id="MYTAG".*?<\/div><\/div><\/div>/smi', '', $FFETADQW);
But since the end tags are not on a single line it doesn't replace anything...


----------------------------

:P Any help would be appreciated
and if this is impossible... let me know... LOL


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Nested Tag Replacement...????

Post by RobertGonzalez »

BuzzCut wrote: I have tried:

Code: Select all

$FFETADQW = preg_replace('/<div id="MYTAG".*?<\/div><\/div><\/div>/smi', '', $FFETADQW);
But since the end tags are not on a single line it doesn't replace anything...
I think there is a multiline operator (\s) that allows your pattern to span more than one line.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Ooh, that's hairy. Is there any reason why you can't use a greedy match (.*)?

Also, why is the regexp missing a >?
I think there is a multiline operator (\s) that allows your pattern to span more than one line.
I believe he already has that set.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I didn't see it in there, and he said he couldn't search more than one line (as was in my quote) so I tossed it out there. If I missed something, sorry, my mistake.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I didn't see it in there, and he said he couldn't search more than one line (as was in my quote) so I tossed it out there. If I missed something, sorry, my mistake.
It's okay. I just had to mention it because if indeed he had been using it, the comment probably would have confused him even more.

I just realized that when Feyd reformatted the post, we lost some important info. Hey BuzzCut, could you repost just the section that gets replaced?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Ambush Commander wrote:I just realized that when Feyd reformatted the post, we lost some important info.
What important info? That's all that was there, just in the appropriate tags.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Oh. Hmm. What does this comment mean then?
It will replace all the text in red below::::
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't recall any red text.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmm... then we'll just have to wait for the poster to reply. Sorry for accusing you Feyd. :oops:
BuzzCut
Forum Newbie
Posts: 2
Joined: Sat Aug 05, 2006 9:59 am

Post by BuzzCut »

Sorry, I posted this in another forum and didn't get any replies.. so I copied and pasted it in here without considering it had PHP Code in it and other things I should have known about before posting.. I apologize.


First of all I am new to PHP, so I don't know MUCH YET... LOL

I don't know what a GREEDY MATCH IS...
"greedy match (.*)?"

--------

It will replace all the text in red below::::
Means that it will replace everything between

Code: Select all

<div id="MYTAG"> and the very first end div tag </div>

What this is - is an RSS reader..
I am trying to replace content I do not want.. as in empty div tags containing other empty div tags.

Sometimes there is info in the divs but more often there is not.. if there isn't data in the divs it messes up the page.

I have been able to replace everything else I want accept for these empty divs.. since they also have WIDTHS in them that do not remain constant. So yea it's HAIRY... for a NEWB...


AGAIN...
ANY help would be greatly appreciated.........

Thanks!
Last edited by BuzzCut on Sun Aug 06, 2006 10:27 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Ambush Commander wrote:Hmm... then we'll just have to wait for the poster to reply. Sorry for accusing you Feyd. :oops:
No problem. I could be equally wrong. I just don't remember seeing red text.

Anyways, running his code, I believe he would have been referring to this:

Code: Select all

[feyd@home]>php -r "$string = '<div id="MYTAG">'.PHP_EOL.'Main Content...'.PHP_EOL.'<div id="NESTEDTAG1">Content'.PHP_EOL.'<div id="NESTEDTAG2">Content'.PHP_EOL.'</div>'.PHP_EOL.'</div>'.PHP_EOL.'</div>'; echo $string, PHP_EOL, '###', PHP_EOL, preg_replace('#<div id="MYTAG".*?</div>#smi', '', $string);"
<div id="MYTAG">
Main Content...
<div id="NESTEDTAG1">Content
<div id="NESTEDTAG2">Content
</div>
</div>
</div>
###

</div>
</div>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Oh, an RSS reader! That makes things real easy. Use PHP's XML functions. Are you running PHP 5 or PHP 4?
I don't know what a GREEDY MATCH IS...
A greedy match is .* without the trailing question mark, although since you're doing RSS feed parsing, that won't work out since it will match all the way to the very last div.
Post Reply