感谢 jpjacobs 的函数使用提示,这里是完整的函数代码加上一个例子:
---This function sanetizes a HTML string so that the following characters will be shown
-- correctly when the output is rendered in a browser:
-- & will be replaced by &
-- < will be replaced by <
-- > will be replaced by >
-- \n will be replaced by <br/>;
-- (more than one space) will be replaced by (as many as required)
-- @param txt the input text which may have HTML formatting characters
-- @return the sanetized HTML code
function sanitize(txt)
txt=txt:gsub("%&","&")
txt=txt:gsub("%<","<")
txt=txt:gsub("%>",">")
txt=txt:gsub("\n","<br/>")
txt=txt:gsub("(% +)", function(c) return " "..(" "):rep(#c-1) end)
return txt
end
text=[[ <html> hello &bye </html> ]]
print("Text='"..text.."'")
print("sanetize='"..sanitize(text).."'")
输出:
Text=' <html> hello &bye </html> '
sanetize=' <html> hello &bye </html> '