Page 1 of 1

Nested Tag Replacement...????

Posted: Sat Aug 05, 2006 10:02 am
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]

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

Posted: Sun Aug 06, 2006 7:39 pm
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.

Posted: Sun Aug 06, 2006 9:06 pm
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.

Posted: Sun Aug 06, 2006 9:21 pm
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.

Posted: Sun Aug 06, 2006 9:23 pm
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?

Posted: Sun Aug 06, 2006 10:00 pm
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.

Posted: Sun Aug 06, 2006 10:03 pm
by Ambush Commander
Oh. Hmm. What does this comment mean then?
It will replace all the text in red below::::

Posted: Sun Aug 06, 2006 10:05 pm
by feyd
I don't recall any red text.

Posted: Sun Aug 06, 2006 10:06 pm
by Ambush Commander
Hmm... then we'll just have to wait for the poster to reply. Sorry for accusing you Feyd. :oops:

Posted: Sun Aug 06, 2006 10:18 pm
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!

Posted: Sun Aug 06, 2006 10:19 pm
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>

Posted: Sun Aug 06, 2006 10:21 pm
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.