VBScript函数正在取出外来字符

时间:2014-02-27 20:27:42

标签: vbscript

我在VBScript for Classic ASP中有一个旧功能,在提交表单时删除非法字符,但它也会删除外来字符并用A * @L等垃圾代替它们。

该功能如下所示:

Private Function stripillegal(fieldcontents)
    if isnull(fieldcontents) then
        stripillegal = ""
    else
        Dim stripped, stripillegal_c, stripillegal_i
        stripped = ""

        if isempty(fieldcontents) then fieldcontents = ""

        fieldcontents = CStr( fieldcontents )
        fieldcontents = Trim( fieldcontents )

        if Len(fieldcontents)>0 then
            for stripillegal_i = 1 to Len(fieldcontents)
                stripillegal_c = asc(mid(fieldcontents, stripillegal_i, 1))

                select case stripillegal_c
                case 39
                    stripped = stripped & "'"
                case 37
                    stripped = stripped & "%"
                case 34 ' quote (34)
                    stripped = stripped & """
                case else
                    stripped = stripped & chr(stripillegal_c)
                end select
                ' response.write stripped & "<br>"
            next
        end if

        stripped = trim(stripped)
        while Right(stripped, 1) = chr(13) OR Right(stripped, 1) = chr(10)
            stripped = left(stripped, len(stripped)-1)
        wend

        stripillegal = stripped
    end if
End Function

我想知道如何告诉它允许使用法语或西班牙语等外国字符。

1 个答案:

答案 0 :(得分:1)

Regular Expressions可以很好地清理这些字符串,同时避免使用外来字符。

更具体地说,这个功能:

Function strClean (strtoclean)
    Dim objRegExp, outputStr
    Set objRegExp = New Regexp

    objRegExp.IgnoreCase = True
    objRegExp.Global = True
    objRegExp.Pattern = "[(?*"",\\<>&#~%{}+_.@:\/!;]+"
    outputStr = objRegExp.Replace(strtoclean, "-")

    objRegExp.Pattern = "\-+"
    outputStr = objRegExp.Replace(outputStr, "-")

    strClean = outputStr
End Function