代码之家  ›  专栏  ›  技术社区  ›  Adam Ramadhan

用php安全过滤html?

  •  0
  • Adam Ramadhan  · 技术社区  · 16 年前
    function validCleanHtml( $unclosedString )
    {
        preg_match_all( "/<([^\/]\w*)>/", $closedString = $unclosedString, $tags );
        for ( $i = count( $tags[1] ) - 1; $i >= 0; $i-- )
        {
            $tag = $tags[1][$i];
            if ( substr_count( $closedString, "</$tag>" ) < substr_count( $closedString, "<$tag>" ) )
                $closedString .= "</$tag>";
        }
        $validTags = "<em><strong>";
        $validClosedString = strip_tags( $closedString, $validTags );
        return $validClosedString;
    
    }
    

    好的,我想要的是启用2个html,em和strong,这只是从xss安全吗?如果没有,我们怎么能保证?

    2 回复  |  直到 13 年前
        1
  •  4
  •   Mitch Dempsey    16 年前

    strip_tags 掌握答案。

    http://us2.php.net/strip_tags

    除了启用某些字段,还可以删除不需要的字段。即: link style , script iframe , frame

        2
  •  12
  •   jasonbar    16 年前

    你有没有看过像这样的解决方案 htmlpurifier ? 您真的不想编写自己的HTML解析器—当然也不想使用正则表达式。