how can i parse

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


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

Without using extensions ...


Recommended Answers

All 4 Replies

You could use a regular expression. Try the preg_replace function.

Member Avatar for iamthwee

Also consider providing a more extensive example...

I mean how deep does your level of nesting go?

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)

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.