Regexp用于清除空的,不必要的HTML标记

jnb*_*nbn 4 php regex replace preg-replace

我在我的一个项目中使用TinyMCE(WYSIWYG)作为默认编辑器,有时它会自动添加<p>&nbsp;</p> , <p> </p>或删除.

我一直在寻找,但我真的找不到用正则表达式清理任何空标签的好方法.

我尝试使用的代码是,

$pattern = "/<[^\/>]*>([\s]?)*<\/[^>]*>/";
$str = preg_replace($pattern, '', $str); 
Run Code Online (Sandbox Code Playgroud)

注意:我也想清除:(

小智 6

试试吧 /<(\w+)>(\s|&nbsp;)*<\/\1>/ .:)