Php 用类去掉div,并将所有其他html保持在

Php 用类去掉div,并将所有其他html保持在,php,html,regex,Php,Html,Regex,我的html内容: $content = <div class="class-name some-other-class"> <p>ack</p> </div> 我知道带标签($content,)将在本例中完成这项工作,但我希望能够使用某个类将div作为目标,并保留其他div等 我知道你不应该通过正则表达式传递html,那么实现这一点的最佳方法是什么呢$(“.class name”).remove();' 你看了吗?我没有试过,但看起来很有希望。

我的html内容:

$content = <div class="class-name some-other-class">
<p>ack</p>
</div>
我知道带标签($content,)将在本例中完成这项工作,但我希望能够使用某个类将div作为目标,并保留其他div等


我知道你不应该通过正则表达式传递html,那么实现这一点的最佳方法是什么呢$(“.class name”).remove();'

你看了吗?我没有试过,但看起来很有希望。这也会删除
p
标记。你想用标记做什么,因为在查询之后,你有了。
<p>ack</p>
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($content); // loads your HTML
$xpath = new DOMXPath($doc);
// returns a list of all links with class containing class-name
$nlist = $xpath->query("div[contains(@class, 'class-name')]");

// Remove the nodes from the xpath query
foreach($nlist as $node) {
   $node->parentNode->removeChild($node);
}

echo $doc->saveHtml();