0

how can i parse

<tag attr="z">
    <tag attr="y">
        <tag attr="x"></tag>
    </tag>
</tag>

to

<tag attr="z">
    [tag=y]
        <tag attr="x"></tag>
    [/tag]
</tag>

Without using extensions ...

THANKS ALLOT :)

4
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by diafol
0

iamtwee, what you mean?

blocblue, regex wont help me here, because i am able to replace the first or the last tags, but not in XML tree.. (sory for bad english)

0

There is a BBCode package in PEAR. I couldn't get it to work, but I'm sure that's just me.

I think you could use preg for this, although it would be a bit complicated:

<tag attr="y"> <-> [tag=y] is ok.
[/tag] -> </tag> is ok.

The only tricky bit is the </tag> -> [/tag]

If you search for <tag... after the opening one and then count the same number of identical closing </tag> until you get to the outer closing </tag> that matches the one you're replacing. This means the XML/(X)HTML has to be well formed and you need to provide for self closing tags, e.g. <br />, <img />, <hr />, <link />, <meta /> (there are more I think).

I'm stopping there - the though of that regex is giving me a nosebleed. How about SimpleXML or building an XML tree? Just typing aloud.

/?EDIT just read your last line - OK NOT XML tree.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.