Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

73
LINES

< > BotCompany Repo | #35 // Escape HTML entities

Lua code

1  
-- Convert accented characters from a given string and return HTML entities in the output string.
2  
-- by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr
3  
-- v. 2.0 -- 2003/06/10 -- Better algorithm, using regular expression
4  
-- v. 1.0 -- 2003/04/19 -- Naive implementation
5  
-- from http://lua-users.org/files/wiki_insecure/users/PhiLho/3EncodeEntities.lua
6  
7  
local entities =
8  
{
9  
--	['&'] = "&amp;",
10  
	['<'] = "&lt;",
11  
	['>'] = "&gt;",
12  
	-- French entities (the most common ones)
13  
	['à'] = "&agrave;",
14  
	['â'] = "&acirc;",
15  
	['é'] = "&eacute;",
16  
	['è'] = "&egrave;",
17  
	['ê'] = "&ecirc;",
18  
	['ë'] = "&euml;",
19  
	['î'] = "&icirc;",
20  
	['ï'] = "&iuml;",
21  
	['ô'] = "&ocirc;",
22  
	['ö'] = "&ouml;",
23  
	['ù'] = "&ugrave;",
24  
	['û'] = "&ucirc;",
25  
	['ÿ'] = "&yuml;",
26  
	['À'] = "&Agrave;",
27  
	['Â'] = "&Acirc;",
28  
	['É'] = "&Eacute;",
29  
	['È'] = "&Egrave;",
30  
	['Ê'] = "&Ecirc;",
31  
	['Ë'] = "&Euml;",
32  
	['Î'] = "&Icirc;",
33  
	['Ï'] = "&Iuml;",
34  
	['Ô'] = "&Ocirc;",
35  
	['Ö'] = "&Ouml;",
36  
	['Ù'] = "&Ugrave;",
37  
	['Û'] = "&Ucirc;",
38  
	['ç'] = "&ccedil;",
39  
	['Ç'] = "&Ccedil;",
40  
	['Ÿ'] = "&Yuml;",
41  
	['«'] = "&laquo;",
42  
	['»'] = "&raquo;",
43  
	['©'] = "&copy;",
44  
	['®'] = "&reg;",
45  
	['æ'] = "&aelig;",
46  
	['Æ'] = "&AElig;",
47  
	['Œ'] = "&OElig;", -- Not understood by all browsers
48  
	['œ'] = "&oelig;", -- Not understood by all browsers
49  
}
50  
51  
function EncodeEntities3(toEncode)
52  
	if toEncode == nil or type(toEncode) ~= "string" then
53  
		return ''
54  
	end
55  
56  
	local EncodeToEntities = function (char)
57  
		local entity = entities[char]
58  
		if entity == nil then
59  
			local code = string.byte(char)
60  
			if code > 127 then
61  
				entity = string.format("&#%d;", code)
62  
			end
63  
		end
64  
		return entity or char
65  
	end
66  
67  
	entities['&'] = "&amp;"
68  
	-- I will replace '(.)' with '([^%c%s%w%p])'
69  
	encodedString = string.gsub(toEncode, '(.)', EncodeToEntities)
70  
	return encodedString
71  
end
72  
73  
return EncodeEntities3(input)

test run  test run with input  download  show line numbers   

Relations

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

2 comment(s) hidden. show

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 1915 [visualize]

Snippet ID: #35
Snippet name: Escape HTML entities
Eternal ID of this version: #35/1
Text MD5: 59b10f4085073c01b1696819367af1a7
Author: stefan
Category: tools
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2013-12-13 23:00:48
Source code size: 1915 bytes / 73 lines
Pitched / IR pitched: Yes / Yes
Views / Downloads: 2669 / 428
Referenced in: [show references]