Urldecode string



Published: 2019-04-16 17:58:26 +0000
Categories: LUA,

Language

LUA

Description

Sometimes, particularly when using LUA OpenResty, in may be necessary to url unencode/unescape a string, so that reserved characters are converted back to their literal representations. This is particularly useful as the value if ngx.var.arg_ variables are not automatically url unencoded

This snippet creates a function to do hex decode each character of a string

Similar to

Based On

Snippet

local urldecode = function(url)
  return (url:gsub("%%(%x%x)", function(x)
    return string.char(tonumber(x, 16))
    end))
end

Usage Example

local urldecode = function(url)
  return (url:gsub("%%(%x%x)", function(x)
    return string.char(tonumber(x, 16))
    end))
end

local s='a%20b%20c'
print(urldecode(s))
-- a b c

-- Or to decode the query string value of argument 'foo' in OpenResty
ngx.print(urldecode(ngx.var.arg_foo))

Keywords

urldecode, uridecode, url, string,

Latest Posts


Copyright © 2022 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk, http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion and http://snippets.bentasker.i2p
hit counter