加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua – decodeURI(luvit)

发布时间:2020-12-15 00:24:55 所属栏目:大数据 来源:网络整理
导读:我想在我的Lua(Luvit)项目中使用decodeURI或decodeURIComponent,就像在 JavaScript中一样. JavaScript的: decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82')// result: привет 很喜欢: require('querystring').urldecode('%D0%BF%D1%80%D0%B8%D0%B
我想在我的Lua(Luvit)项目中使用decodeURI或decodeURIComponent,就像在 JavaScript中一样.

JavaScript的:

decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82')
// result: привет

很喜欢:

require('querystring').urldecode('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82')
-- result: '%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82'

解决方法

如果您理解 URI percent-encoded format,那么在Lua中做这件事是微不足道的.每个%XX子字符串表示用%前缀和十六进制八位字节编码的UTF-8数据.
local decodeURI
do
    local char,gsub,tonumber = string.char,string.gsub,tonumber
    local function _(hex) return char(tonumber(hex,16)) end

    function decodeURI(s)
        s = gsub(s,'%%(%x%x)',_)
        return s
    end
end

print(decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82'))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读