string - Lua SQL 转义字符串(尝试) '"附近未完成的字符串}'

标签 string lua escaping

function escape_sqli(source)
    to_replace = {"'", '"'}
    replace_with = {"\'", '\"'}
    output = source
    for i = 1, table.getn(to_replace) do
        output = string.gsub(output, to_replace[i], replace_with[i])
    end
    return output
end

我尝试使用上面的代码来转义 SQLis,但是当我尝试编译它时出现以下错误:

Unfinished String near '"}'

最佳答案

就目前而言,the code 中不存在语法错误。 .


不过是一个建议;来自 string.gsub文档:

string.gsub (s, pattern, repl [, n])

[...]

If repl is a table, then the table is queried for every match, using the first capture as the key.

您可以简单地重新创建替换表,如下所示:

local replacements = { ['"'] = '\\"', ["'"] = "\\'" }

并在单个 gsub 调用中使用它:

function escape_sqli(source)
    local replacements = { ['"'] = '\\"', ["'"] = "\\'" }
    return source:gsub( "['\"]", replacements ) -- or string.gsub( source, "['\"]", replacements )
end

关于string - Lua SQL 转义字符串(尝试) '"附近未完成的字符串}',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795538/

相关文章:

windows - 批处理文件中的特殊字符

c# - string.LastIndexOf() 错误?

java - 将 MultiLabelDataset<String, String> 转换为 Guava Multimap 用于打印/检查

c++ - 如何检查 SWIG 接口(interface)文件中的 Lua 版本?

lua - 如何在 Lua 中精确匹配 n 次重复的模式?

javascript - 这是做什么的???/[\\/]electron-prebuilt[\\/]/.test()

string - 如何检查字符串中名称的出现?

c# - 最小化C#中的字符串长度

lua - 通过引用与 ipairs 循环访问表的性能

html - 如何在 <pre> 标签内转义 < 和 >