如何删除像onclick& amp;来自输入的一些html标签

时间:2012-01-10 19:43:35

标签: php html

我正在接受用户的输入,我想要禁用几个html标签,并且还想禁用onclick,onmousemove,onmouseover等属性

用户仍然可以编写像html php jQuery这样的编程语言代码供讨论。但它不应该运行

请指导我

1 个答案:

答案 0 :(得分:5)

这是我用过的一个功能,你可以根据自己的需要定制。

function strip_unsafe($string, $img=false)
{
    // Unsafe HTML tags that members may abuse
    $unsafe=array(
    '/<iframe(.*?)<\/iframe>/is',
    '/<title(.*?)<\/title>/is',
    '/<pre(.*?)<\/pre>/is',
    '/<frame(.*?)<\/frame>/is',
    '/<frameset(.*?)<\/frameset>/is',
    '/<object(.*?)<\/object>/is',
    '/<script(.*?)<\/script>/is',
    '/<embed(.*?)<\/embed>/is',
    '/<applet(.*?)<\/applet>/is',
    '/<meta(.*?)>/is',
    '/<!doctype(.*?)>/is',
    '/<link(.*?)>/is',
    '/<body(.*?)>/is',
    '/<\/body>/is',
    '/<head(.*?)>/is',
    '/<\/head>/is',
    '/onload="(.*?)"/is',
    '/onunload="(.*?)"/is',
    '/<html(.*?)>/is',
    '/<\/html>/is');

    // Remove graphic too if the user wants
    if ($img==true)
    {
        $unsafe[]='/<img(.*?)>/is';
    }

    // Remove these tags and all parameters within them
    $string=preg_replace($unsafe, "", $string);

    return $string;
}