如何在php中取消嵌套嵌套列表

时间:2021-10-20 14:34:12

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 ?

使用PHP,有没有一种很好的方法来“爆炸”这个嵌套列表(对于这个例子)3个列表?

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>

1 个解决方案

#1


-1  

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.
}

#1


-1  

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.
}