PHP 解析类以清理所见即所得的 html 输入(特别是字体属性)


PHP parsing class to clean WYSIWYG html input (specifically font attributes)

我正在使用 Redactor WYSIWYG 编辑器,但我发现它并不总是像它应该的那样清理粘贴上的输入。

所以我们最终得到这样的事情:

<p><span style="font:12px arial; margin: 10px">blah blah blah</span></p>
<pstyle="font-family:Georgia: font-size:14px">more blah blah</span></p>

我只想清理属性,特别是 p、a、div 或 span 标签上的style=""属性。

是否有可以使用的可靠类或正则表达式?

试试 DOMDocument:

使用 PHP 的 HTML DOMDocument 解析 HTML

http://php.net/manual/en/class.domdocument.php

编辑:顺便说一句,您可能还应该重新考虑您正在使用的所见即所得编辑器。我一直在使用 ckeditor、tinymce 以及最近与 aloha 编辑器合作,这三个编辑器都按预期解析数据。此外,这三种方法都可以根据自己的喜好解析粘贴数据。