类似于%u4E92%u52A8这种格式的,如何在LUA中还原为汉字?

351 views
Skip to first unread message

lhmwzy

unread,
Jul 24, 2013, 4:29:42 AM7/24/13
to open...@googlegroups.com
user_name\":\"%u4E92%u52A8%u6CE8%u518C\",\"address_line\":\"%u6D4E%u5357%u69D0%u836B%u533A%u7ECF%u5341%u8DEF28293%u53F73%u53F7%u697C3-602

这种格式,如何在LUA中还原成汉字?

wd

unread,
Jul 24, 2013, 7:00:37 AM7/24/13
to open...@googlegroups.com
找找 urldecode


On Wed, Jul 24, 2013 at 4:29 PM, lhmwzy <lhm...@gmail.com> wrote:
user_name\":\"%u4E92%u52A8%u6CE8%u518C\",\"address_line\":\"%u6D4E%u5357%u69D0%u836B%u533A%u7ECF%u5341%u8DEF28293%u53F73%u53F7%u697C3-602

这种格式,如何在LUA中还原成汉字?

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
 
 

zachary hu

unread,
Jul 24, 2013, 9:27:14 AM7/24/13
to open...@googlegroups.com
  1. function urlencode(str)  
  2.     if (str) then  
  3.         str = string.gsub (str, "\n""\r\n")  
  4.         str = string.gsub (str, "([^%w ])",  
  5.         function (c) return string.format ("%%%02X", string.byte(c)) end)  
  6.         str = string.gsub (str, " ""+")  
  7.     end  
  8.     return str  
  9. end  
  10. function urldecode(str)  
  11.   str = string.gsub (str, "+"" ")  
  12.   str = string.gsub (str, "%%(%x%x)",  
  13.       function(h) return string.char(tonumber(h,16)) end)  
  14.   str = string.gsub (str, "\r\n""\n")  
  15.   return str  
  16. end  
难道是这个- -! ly大,明天去试试

lhmwzy

unread,
Jul 24, 2013, 10:27:30 AM7/24/13
to open...@googlegroups.com
这个不是,是urlencode和urldecode

Yichun Zhang (agentzh)

unread,
Jul 24, 2013, 2:35:34 PM7/24/13
to openresty
Hello!

On Wed, Jul 24, 2013 at 1:29 AM, lhmwzy wrote:
> user_name\":\"%u4E92%u52A8%u6CE8%u518C\",\"address_line\":\"%u6D4E%u5357%u69D0%u836B%u533A%u7ECF%u5341%u8DEF28293%u53F73%u53F7%u697C3-602
>
> 这种格式,如何在LUA中还原成汉字?
>

这里使用了非标准的 Unicode URI 编码格式 %uXXXX(据说这种格式已经被 W3C 拒了)。所以,使用 ngx_lua 内建的
ngx.decode_uri() 是无法处理的,因为 ngx_lua 使用的是 nginx 核心中的 URI
解码器,只支持标准的以字节为单位的 URI 编码格式。

你需要自己对这种东西进行解析,并自己把 Unicode code point 自己转换为对应的字符编码,比如 UTF-8.
我不确定是否有现成的第三方 Lua 库支持这种东西 :)

Regards,
-agentzh

zachary hu

unread,
Jul 24, 2013, 9:02:41 PM7/24/13
to open...@googlegroups.com
春哥,对方AXIS POST了一段XML过来,中间有中文大致:
内嵌图片 1
在get_body_data()获取到的是{&quot;customer_id&quot;:&quot;1118999999722279599&quot;,&quot;user_id&quot;:&quot;9223372029417726265&quot;,&quot;user_name&quot;:&quot;&#x4E92;&#x52A8;&#x6CE8;&#x518C;&quot;,&quot;address_line&quot;:&quot;&#x6D4E;&#x5357;&#x69D0;&#x836B;&#x533A;&#x7ECF;&#x5341;&#x8DEF;28293&#x53F7;3&#x53F7;&#x697C;3-602&quot;
这样的编码字串,直接xml.eval中间的中文之后就无法正常解析了,请教一下怎么处理比较合适。我是需要把中间这段json中文插入db的




image.png

Yichun Zhang (agentzh)

unread,
Jul 24, 2013, 9:45:07 PM7/24/13
to openresty
Hello!

2013/7/24 zachary hu
>
> 春哥,对方AXIS POST了一段XML过来,中间有中文大致:
>
> 在get_body_data()获取到的是{&quot;customer_id&quot;:&quot;1118999999722279599&quot;,&quot;user_id&quot;:&quot;9223372029417726265&quot;,&quot;user_name&quot;:&quot;&#x4E92;&#x52A8;&#x6CE8;&#x518C;&quot;,&quot;address_line&quot;:&quot;&#x6D4E;&#x5357;&#x69D0;&#x836B;&#x533A;&#x7ECF;&#x5341;&#x8DEF;28293&#x53F7;3&#x53F7;&#x697C;3-602&quot;
> 这样的编码字串,直接xml.eval中间的中文之后就无法正常解析了,请教一下怎么处理比较合适。我是需要把中间这段json中文插入db的
>

貌似你用的是 LuaXML 库?这个库貌似并不怎么靠谱的样子。换一个靠谱点的 Lua XML
库吧,呵呵。等我有时间,我也想自己弄一个靠谱点的,直接放在 openresty 里面 :) XML 确实还是很常见的东西。

Regards,
-agentzh

zachary hu

unread,
Jul 24, 2013, 9:48:08 PM7/24/13
to open...@googlegroups.com
现在有推荐的么?貌似作者是完全没有考虑过中文的问题的,查过很多都貌似只有这一个可以用。。。



Regards,
-agentzh

xuanbei

unread,
Jul 24, 2013, 9:50:25 PM7/24/13
to open...@googlegroups.com, xuanbei
恩同需要啊,谁用过比较不错的推荐一下

zachary hu

unread,
Jul 25, 2013, 4:40:15 AM7/25/13
to open...@googlegroups.com
群里一个靠谱青年研究了半天写了两个方法解决了我的需求。。。。我就差要做UTF8 unicode字典来自解析了。。
 local char = string.char
 
local function convert(n, k)
  local u, r=''
  for i=1,k do
    n,r = math.floor(n/0x40), n%0x40
    u = char(r+0x80) .. u
  end
  return u, n
end
 
local function utu(a)
  local n, r, u = tonumber(a,16)
  if n<0x80 then
    return char(n)
  elseif n<0x800 then
    u, n = convert(n, 1)
    return char(n+0xc0) .. u
  elseif n<0x10000 then
    u, n = convert(n, 2)
    return char(n+0xe0) .. u
  elseif n<0x200000 then
    u, n = convert(n, 3)
    return char(n+0xf0) .. u
  elseif n<0x4000000 then
    u, n = convert(n, 4)
    return char(n+0xf8) .. u
  else
    u, n = convert(n, 5)
    return char(n+0xfc) .. u
  end
end
str="%u6D4E%u5357%u69D0%u836B%u533A%u7ECF%u5341%u8DEF28293%u53F73%u53F7%u697C3-602"
ngx.say(string.gsub(str, '%%u(%w%w%w%w)', utu)) 

wgm

unread,
Nov 18, 2013, 3:03:19 AM11/18/13
to open...@googlegroups.com
支持春哥做一个XML放在openresty里,最近也在与luaxml做斗争。
Reply all
Reply to author
Forward
0 new messages