• PHP实例——输出安全的HTML代码


    //输出安全的html
    function h($text, $tags = null){

    $text = trim($text);
    //完全过滤注释
    $text = preg_replace('/<!--?.*-->/','',$text);

    //完全过滤动态代码
    $text = preg_replace('/<\?|\?'.'>/','',$text);

    //完全过滤js
    $text = preg_replace('/<script?.*\/script>/','',$text);


    $text = str_replace('[','&#091;',$text);
    $text = str_replace(']','&#093;',$text);
    $text = str_replace('|','&#124;',$text);
    //过滤换行符
    $text = preg_replace('/\r?\n/','',$text);

    //br
    $text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);

    $text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
    //过滤危险的属性,如:过滤on事件lang js
    while(preg_match('/(<[^><]+)( lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){

    $text=str_replace($mat[0],$mat[1],$text);
    }
    while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
    $text=str_replace($mat[0],$mat[1].$mat[3],$text);
    }
    if(empty($tags)) {
    $tags = 'table|td|th|tr|i|b|u|strong|img|p|br|div|strong|em|ul|ol|li|dl|dd|dt|a';
    }
    //允许的HTML标签
    $text = preg_replace('/<('.$tags.')( [^><\[\]]*)>/i','[\1\2]',$text);

    //过滤多余html
    $text = preg_replace('/<\/?(html|head|meta|link|base|basefont|body|bgsound|title|style|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name|script|style|xml)[^><]*>/i','',$text);

    //过滤合法的html标签
    while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){

    $text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
    }
    //转换引号
    while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){

    $text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
    }
    //过滤错误的单个引号
    while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){

    $text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
    }
    //转换其它所有不合法的 < >
    $text = str_replace('<','&lt;',$text);

    $text = str_replace('>','&gt;',$text);
    $text = str_replace('"','&quot;',$text);
    //反转换
    $text = str_replace('[','<',$text);

    $text = str_replace(']','>',$text);
    $text = str_replace('|','"',$text);
    //过滤多余空格
    $text = str_replace(' ',' ',$text);

    return $text;
    }
  • 相关阅读:
    VisualStudio 2012中的单元测试
    基于.Net 写我自己的Ajax后台框架AjaxFramework
    MongoDB的真正性能实战百万用户
    基于MMSeg算法的中文分词类库
    Visual Studio 2012的新插件Code Digger (类似Pex)
    Visual Studio 2010 单元测试之一普通单元测试
    Django系列教程:一、Django的安装和入门
    Cocos2dx 入门调研总结
    教程:建立自己的私有云
    如何让你的作业在Hadoop集群中真正实现分布式运行
  • 原文地址:https://www.cnblogs.com/picaso/p/2408877.html
Copyright © 2020-2023  润新知