首页 > 解决方案 > Lua 白名单仅清理和允许干净的 URL 字符串

问题描述

因此,我试图提出一种方法,该方法仅允许使用干净的 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

脏输出:(这是我想要摆脱的,因为字符未列入白名单或多个不必要的斜杠)

com/url///////index.php/html//////path///////stuff

com/url///////+index.php/=html//////path///////stuff@[

标签: lua

解决方案


我不确定你是什么意思。尝试这个。

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

推荐阅读