我有一个动态菜单,看起来像
cat1- test5
- cat2
- cat9
- cat7
- cat8
- cat10
- cat1 cat2
- cat5
我想将其更改为正确嵌套的导航菜单
cat1 cat3我尝试了几个str_replace但由于列表是动态的它不会工作.
我是Regex的新手,我不知道如何将这个动态菜单格式化为正确嵌套/格式化的菜单.
提前致谢!
解决方法:
$dom = new DOMDocument;
$dom->loadHTML($html);
$arrLi = array();
foreach ($dom->getElementsByTagName('li') as $tag) {
$arrLi[$tag->nodeValue]="$tag->nodeValue";
}
echo $html = strtr($html,$arrLi);
标签:php,arrays,function,dom,regex
来源: https://codeday.me/bug/20190830/1770952.html