如何删除所有HTML标记排除某些标记

时间:2013-05-03 16:31:52

标签: php html tags removeall

我创建了一个表单,我希望使用PHP删除所有HTML标记,但排除某些标记(<b>, <strong>, <em>, <i>, <p>, <br>, <ul>, <li> <ol> ...(以及格式段落的某些标记)当成员点击提交时,它将被插入数据库。

$content = $_POST['content'];

谢谢大家的帮助 如果我的英语不好,我很抱歉。

3 个答案:

答案 0 :(得分:12)

这是你在找什么?

$content=strip_tags($content,"<b><strong><em><i><p><br><ul><li><ol>");

答案 1 :(得分:3)

以下应该这样做:

// tags separated by vertical bar
$strip_tags = "a|strong|em";

// target html
$html = '<em><a><b>ha<a href="" title="">d</a>f</em></b>';

// Regex is loose and works for closing/opening tags across multiple lines and
// is case-insensitive
// note: The *? makes the matching non-greedy
$clean_html = preg_replace("#<\s*\/?(".$strip_tags.")\s*[^>]*?>#im", '', $html);

// prints "<b>hadf</b>";
echo $html;

答案 2 :(得分:2)

使用strip_tags()可能会有危险,因为它不会查看HTML属性。因此,恶意用户可以将其用于跨站点脚本(XSS)以及其他攻击(在我对David Chen的评论中也有提及)。

相反,我建议使用现有的HTML过滤器,例如http://htmlpurifier.org/,它可能更安全,更适合此任务。

相关问题