Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua白名单,用于清理并仅允许清除URL字符串_Lua - Fatal编程技术网

Lua白名单,用于清理并仅允许清除URL字符串

Lua白名单,用于清理并仅允许清除URL字符串,lua,Lua,因此,我试图想出一种方法,只允许使用干净的URL,如果字符串包含任何不应该包含的字符,则将其置为空 我只希望url接受的字符白名单 A-Z a-z 0-9 _ - . / Lua代码示例: local bar = "com/url/index.php/html/path/stuff.html.html123/..lol" bar = bar:gsub("%.html.*$","") bar = bar:gsub("%/$","") bar = bar:gsub("%.$","") print(

因此,我试图想出一种方法,只允许使用干净的URL,如果字符串包含任何不应该包含的字符,则将其置为空

我只希望url接受的字符白名单

A-Z a-z 0-9 _ - . /
Lua代码示例:

local bar = "com/url/index.php/html/path/stuff.html.html123/..lol"
bar = bar:gsub("%.html.*$","")
bar = bar:gsub("%/$","")
bar = bar:gsub("%.$","")
print(bar)
--TODO: if characters not in whitelist then make bar empty
bar = ""
清洁输出:

com/url/index.php/html/path/stuff
脏输出:(这是我想要摆脱的,因为字符没有白名单或多个不必要的斜杠)


我不知道你的意思。试试这个

if bar:gsub("[A-Za-z0-9_%-%./]","")=="" then
    -- bar is ok
else
    bar=""
end

谢谢你的回答,它是有效的。剩下的唯一问题是,你知道我如何解决如上所示的多斜杠问题吗?当出现
string.find()
时,为什么要浪费内存分配新字符串?
if bar:gsub("[A-Za-z0-9_%-%./]","")=="" then
    -- bar is ok
else
    bar=""
end