首页 > 解决方案 > PHP使用特定标签删除div及其子元素

问题描述

PHP中有没有办法从内容中删除具有特定类的所有div并返回结果,例如我有这个代码:

<?php
$content = '<div class="toRemove">
content here 
bla bla
<div class="toBeRemoved">content here also</div>
<div class="test">content ...</div>
lorem ipsum dolor sit ...
other divs here with different classes and ids
</div>
<div class="otherInfo">....</div>
';
$content = preg_replace('#<div class="toRemove">([\s\S]*?)</div>#','', $content); // tried this but did work as I wanted to
echo $content;

我想要的是使用 toRemove 类删除 div 内的所有内容,我得到的是未删除子 div 的内容!

提前感谢您的帮助。

标签: phpregex

解决方案


推荐阅读