主题: PHP过滤不安全的HTML代码
作者: Roddy, 发布日期: 2008-12-23 22:56:12, 浏览数: 6908

php.net strip_tags函数评论里的方法,不是很好。

参考地址:http://cn2.php.net/manual/en/function.strip-tags.php

 
function get_safe_html($html)
{
    $allowedTags = array(
                         '<a>', '<font>', '<span>', '<p>', '<br>', '<div>', '<li>', '<u>', '<strike>',
                         '<strong>', '<table>', '<tr>', '<td>', '<tbody>', '<hr>', '<blockquote>',
                         '<sub>', '<sup>', '<ul>', '<ol>', '<img>', '<b>', '<em>', '<h1>', '<h2>', '<h3>',
                         '<h4>', '<h5>', '<h6>'
                         );
    return strip_tags_attributes($html, $allowedTags);
}
function strip_tags_attributes($html, $allowedTags = array())
{
    $disabledAttributes = array(
                                 'onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate',
                                 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus',
                                 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur',
                                 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu',
                                 'oncontrolselect', 'oncopy', 'oncut', 'ondataavaible', 'ondatasetchanged',
                                 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragdrop',
                                 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart',
                                 'ondrop', 'onerror', 'onerrorupdate', 'onfilterupdate', 'onfinish', 'onfocus',
                                 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup',
                                 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter',
                                 'onmouseleave', 'onmousemove', 'onmoveout', 'onmouseover', 'onmouseup',
                                 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste',
                                 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend',
                                 'onresizestart', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll',
                                 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit',
                                 'onunload');
    return preg_replace('/\s(' . implode('|', $disabledAttributes) . ').*?([\s\>])/', '\\2', preg_replace('/<(.*?)>/ie', "'<' . preg_replace(array('/javascript:[^\"\']*/i', '/(" . implode('|', $disabledAttributes) . ")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", strip_tags($html, implode('', $allowedTags))) );
}

作者: none, 发布日期: 2009-01-18 03:29:10
没必要这样写哦
可以直接过滤掉的
全部转义成安全字符就OK
回复
作者: Ken, 发布日期: 2009-10-11 23:42:06

完全没必要。

我的方法直接将<和>转成4字节的html标记。

回复
作者: Roddy, 发布日期: 2009-10-12 09:34:23
To Ken:
问题是怎么挑选出要转换的HTML标签呢,因为有些HTML标签要保留的,否则就没有可视化效果了。
最近发现了一个好用的库,用下来非常不错,htmlpurifier.org/
回复
作者: combo, 发布日期: 2010-10-05 17:45:46
感谢无私分享啊,支持一个。
回复
发表新帖 发表回复