preg_replace html标签

时间:2011-05-14 15:07:31

标签: php regex preg-replace

我想在字符串上删除所有html标记(包括其属性,如class,src,id等),但我想保留<ul><li>和{{1标签。我将如何实现这一目标?

任何建议都将不胜感激。

谢谢!

4 个答案:

答案 0 :(得分:6)

使用strip_tags()并传入允许的标签。无需重新发明轮子。此方法存在一些安全问题,可能会阻止您使用它。如果是这种情况,请考虑@Denis' answer

答案 1 :(得分:3)

http://us.php.net/manual/en/function.strip-tags.php

strip_tags($ text,'&lt; ul&gt;&lt; li&gt;&lt; br&gt;');

答案 2 :(得分:3)

您可以使用html净化器等库:

http://htmlpurifier.org/

警惕带有参数的strip_tags():

Is strip_tags() vulnerable to scripting attacks?

http://htmlpurifier.org/comparison

答案 3 :(得分:1)

你去,兄弟

http://us.php.net/manual/en/function.strip-tags.php

$text = strip_tags($text, '<ul><li><br>');

不要使用preg_replace,这会耗费大量内存。

相关问题