用特定类别替换UL标签 [英] Replace UL tags with specific class

查看:166
本文介绍了用特定类别替换UL标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所有ul标签替换为level0类,如下所示:

I'm trying to replace all ul tags with a level0 class, something like this:

<ul>
    <li>Test
         <ul class="level0">
           ...
         </ul>
     </li>
</ul>

将被处理为

<ul>
    <li>Test</li>
</ul>

我尝试了

$_menu = preg_replace('/<ul class="level0">(.*)<\/ul>/iU', "", $_menu); 

但是它不起作用,有帮助吗?

but it's not working, help?

谢谢.

耶希亚

推荐答案

我确定这是重复的,但是无论如何,这是使用

I am sure this is a duplicate, but anyway, here is how to do it with DOM

$dom = new DOMDocument;                          // init new DOMDocument
$dom->loadHTML($html);                           // load HTML into it
$xpath = new DOMXPath($dom);                     // create a new XPath
$nodes = $xpath->query('//ul[@class="level0"]'); // Find all UL with class
foreach($nodes as $node) {                       // Iterate over found elements
    $node->parentNode->removeChild($node);       // Remove UL Element
}
echo $dom->saveHTML();                           // output cleaned HTML

这篇关于用特定类别替换UL标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆