views:

163

answers:

1

I have a nested unordered list like this (simplified version / the depth is variable) :

    <ul>
    <li>
    <a href="#">Root</a>
        <ul>
            <li>
                <a href="#">Page A</a>
                <ul>
                    <li>
                    <a href="#" title="Page A1">Page 1 2</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    </ul>

Using PHP, is there a nice way to "explode" this nested list in (for this example) 3 lists ?

Thanks for your help

Edit :

The expected output will be :

<ul>
    <li>
    <a href="#">Root</a>
    </li>
</ul>

<ul>
    <li>
        <a href="#">Page A</a>
    </li>
</ul>

<ul>
    <li>
    <a href="#" title="Page A1">Page 1 2</a>
    </li>
</ul>
A: 

Try:

$lists = '<ul>
<li>
<a href="#">Root</a>
    <ul>
        <li>
            <a href="#">Page A</a>
            <ul>
                <li>
                <a href="#" title="Page A1">Page 1 2</a>
                </li>
            </ul>
        </li>
    </ul>
</li>
</ul>';

$list_array = explode('<ul>', $lists);
foreach($list_array as $list){
    // Now you have a single list, but missing the <ul>
    // at the start. replace that and assign to a variable or whatever.
}
menkes
That won't produce the desired output -- the first two results will have no closing tags, and the 3rd will have 3 sets of closing tags.
Frank Farmer
Yes, and there's something else : if each list has multiple items it won't work
Fries
Quite right. Coffee was still brewing ... I need to be patient and wait to drink some in the morning.
menkes