PDA

View Full Version : example lua code to convert string to/from utf8 in girder



mhwlng
August 1st, 2008, 02:58 PM
A lot of web based stuff needs strings in utf-8 format (e.g. last.fm track submission)

Netremote has built-in lua string character set conversion functions.

Girder doesn't have these, but this works :




require 'htmltidy'

function stringconvert (instr,fromencoding,toencoding)
local c = htmltidy.new()
c:setOpt(htmltidy.opt.XmlTags, true)
c:setOpt(htmltidy.opt.NumEntities, false)
c:setOpt(htmltidy.opt.WrapLen, 9999)
c:setOpt(htmltidy.opt.ForceOutput, true)
c:setOpt(htmltidy.opt.InCharEncoding, fromencoding)
c:setOpt(htmltidy.opt.OutCharEncoding, toencoding)
c:parse("<![CDATA["..instr.."]]>")
local _,_,resultstr = string.find (c:saveToString(),"<!%[CDATA%[(.-)%]%]>");
return resultstr
end

print (stringconvert ("Anas Mitchell,"latin1","utf8"))

Anïs Mitchell




Marcel