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 | -- ['&'] = "&", |
10 | ['<'] = "<", |
11 | ['>'] = ">", |
12 | -- French entities (the most common ones) |
13 | ['à'] = "à", |
14 | ['â'] = "â", |
15 | ['é'] = "é", |
16 | ['è'] = "è", |
17 | ['ê'] = "ê", |
18 | ['ë'] = "ë", |
19 | ['î'] = "î", |
20 | ['ï'] = "ï", |
21 | ['ô'] = "ô", |
22 | ['ö'] = "ö", |
23 | ['ù'] = "ù", |
24 | ['û'] = "û", |
25 | ['ÿ'] = "ÿ", |
26 | ['À'] = "À", |
27 | ['Â'] = "Â", |
28 | ['É'] = "É", |
29 | ['È'] = "È", |
30 | ['Ê'] = "Ê", |
31 | ['Ë'] = "Ë", |
32 | ['Î'] = "Î", |
33 | ['Ï'] = "Ï", |
34 | ['Ô'] = "Ô", |
35 | ['Ö'] = "Ö", |
36 | ['Ù'] = "Ù", |
37 | ['Û'] = "Û", |
38 | ['ç'] = "ç", |
39 | ['Ç'] = "Ç", |
40 | ['Ÿ'] = "Ÿ", |
41 | ['«'] = "«", |
42 | ['»'] = "»", |
43 | ['©'] = "©", |
44 | ['®'] = "®", |
45 | ['æ'] = "æ", |
46 | ['Æ'] = "Æ", |
47 | ['Œ'] = "Œ", -- Not understood by all browsers |
48 | ['œ'] = "œ", -- 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['&'] = "&" |
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
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
ID | Author/Program | Comment | Date | |
---|---|---|---|---|
963 | #1000604 (pitcher) | 2015-08-20 15:28:24 | ||
962 | #1000610 | Edit suggestion: !636 !629 main { static Object androidContext; static String programID; public static void main(String[] args) throws Exception { -- Convert accented characters from a given string and return HTML entities in the output string. -- by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr -- v. 2.0 -- 2003/06/10 -- Better algorithm, using regular expression -- v. 1.0 -- 2003/04/19 -- Naive implementation -- from http://lua-users.org/files/wiki_insecure/users/PhiLho/3EncodeEntities.lua local entities = { -- ['&'] = "&", ['<'] = "<", ['>'] = ">", -- French entities (the most common ones) ['à '] = "à", ['â'] = "â", ['é'] = "é", ['è'] = "è", ['ê'] = "ê", ['ë'] = "ë", ['î'] = "î", ['ï'] = "ï", ['ô'] = "ô", ['ö'] = "ö", ['ù'] = "ù", ['û'] = "û", ['ÿ'] = "ÿ", ['Ã?'] = "À", ['Ã?'] = "Â", ['Ã?'] = "É", ['Ã?'] = "È", ['Ã?'] = "Ê", ['Ã?'] = "Ë", ['Ã?'] = "Î", ['Ã'] = "Ï", ['Ã?'] = "Ô", ['Ã?'] = "Ö", ['Ã?'] = "Ù", ['Ã?'] = "Û", ['ç'] = "ç", ['Ã?'] = "Ç", ['Ÿ'] = "Ÿ", ['«'] = "«", ['»'] = "»", ['©'] = "©", ['®'] = "®", ['æ'] = "æ", ['Ã?'] = "Æ", ['Å?'] = "Œ", -- Not understood by all browsers ['Å?'] = "œ", -- Not understood by all browsers } function EncodeEntities3(toEncode) if toEncode == nil or type(toEncode) ~= "string" then return '' end local EncodeToEntities = function (char) local entity = entities[char] if entity == nil then local code = string.byte(char) if code > 127 then entity = string.format("&#%d;", code) end end return entity or char end entities['&'] = "&" -- I will replace '(.)' with '([^%c%s%w%p])' encodedString = string.gsub(toEncode, '(.)', EncodeToEntities) return encodedString end return EncodeEntities3(input) }} | 2015-08-20 08:33:07 | delete |
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: | 2151 / 327 |
Referenced in: | [show references] |