Convert between HTML entities and special characters

Ask questions about how to create a script or swap scripts with other users.
Post Reply
User avatar
Rickard Johansson
Site Admin
Posts: 6001
Joined: 19 Jul 2006 14:29

Convert between HTML entities and special characters

Post by Rickard Johansson »

A simple script to convert HTML entities to characters or special characters to HTML entities. This is a modified script created by Walti.

A dialog window let you choose which type of conversion you want to make.

This script will only work with version 8.65, or later.

Code: Select all

var entities[200];
var chars[200];

function fillArrays()
{
   chars[0]  = "\"";entities[0]   = """;
   chars[1]  = "'"; entities[1]   = "'";
   chars[2]  = "¡"; entities[2]   = "¡";
   chars[3]  = "¢"; entities[3]   = "¢";
   chars[4]  = "£"; entities[4]   = "£";
   chars[5]  = "¤"; entities[5]   = "¤";
   chars[6]  = "¥"; entities[6]   = "¥";
   chars[7]  = "¦"; entities[7]   = "¦";
   chars[8]  = "§"; entities[8]   = "§";
   chars[9]  = "¨"; entities[9]   = "¨";
  chars[10]  = "©"; entities[10]  = "©";
  chars[11]  = "ª"; entities[11]  = "ª";
  chars[12]  = "«"; entities[12]  = "«";
  chars[13]  = "¬"; entities[13]  = "¬";
  chars[14]  = "®"; entities[14]  = "®";
  chars[15]  = "¯"; entities[15]  = "¯";
  chars[16]  = "°"; entities[16]  = "°";
  chars[17]  = "±"; entities[17]  = "±";
  chars[18]  = "²"; entities[18]  = "²";
  chars[19]  = "³"; entities[19]  = "³";
  chars[20]  = "´"; entities[20]  = "´";
  chars[21]  = "µ"; entities[21]  = "µ";
  chars[22]  = "¶"; entities[22]  = "¶";
  chars[23]  = "·"; entities[23]  = "·";
  chars[24]  = "¸"; entities[24]  = "¸";
  chars[25]  = "¹"; entities[25]  = "¹";
  chars[26]  = "º"; entities[26]  = "º";
  chars[27]  = "»"; entities[27]  = "»";
  chars[28]  = "¼"; entities[28]  = "¼";
  chars[29]  = "½"; entities[29]  = "½";
  chars[30]  = "¾"; entities[30]  = "¾";
  chars[31]  = "¿"; entities[31]  = "¿";
  chars[32]  = "À"; entities[32]  = "À";
  chars[33]  = "Á"; entities[33]  = "Á";
  chars[34]  = "Â"; entities[34]  = "Â";
  chars[35]  = "Ã"; entities[35]  = "Ã";
  chars[36]  = "Ä"; entities[36]  = "Ä";
  chars[37]  = "Å"; entities[37]  = "Å";
  chars[38]  = "Æ"; entities[38]  = "Æ";
  chars[39]  = "Ç"; entities[39]  = "Ç";
  chars[40]  = "È"; entities[40]  = "È";
  chars[41]  = "É"; entities[41]  = "É";
  chars[42]  = "Ê"; entities[42]  = "Ê";
  chars[43]  = "Ë"; entities[43]  = "Ë";
  chars[44]  = "Ì"; entities[44]  = "Ì";
  chars[45]  = "Í"; entities[45]  = "Í";
  chars[46]  = "Î"; entities[46]  = "Î";
  chars[47]  = "Ï"; entities[47]  = "Ï";
  chars[48]  = "Ð"; entities[48]  = "Ð";
  chars[49]  = "Ñ"; entities[49]  = "Ñ";
  chars[50]  = "Ò"; entities[50]  = "Ò";
  chars[51]  = "Ó"; entities[51]  = "Ó";
  chars[52]  = "Ô"; entities[52]  = "Ô";
  chars[53]  = "Õ"; entities[53]  = "Õ";
  chars[54]  = "Ö"; entities[54]  = "Ö";
  chars[55]  = "×"; entities[55]  = "×";
  chars[56]  = "Ø"; entities[56]  = "Ø";
  chars[57]  = "Ù"; entities[57]  = "Ù";
  chars[58]  = "Ú"; entities[58]  = "Ú";
  chars[59]  = "Û"; entities[59]  = "Û";
  chars[60]  = "Ü"; entities[60]  = "Ü";
  chars[61]  = "Ý"; entities[61]  = "Ý";
  chars[62]  = "Þ"; entities[62]  = "Þ";
  chars[63]  = "ß"; entities[63]  = "ß";
  chars[64]  = "à"; entities[64]  = "à";
  chars[65]  = "á"; entities[65]  = "á";
  chars[66]  = "â"; entities[66]  = "â";
  chars[67]  = "ã"; entities[67]  = "ã";
  chars[68]  = "ä"; entities[68]  = "ä";
  chars[69]  = "å"; entities[69]  = "å";
  chars[70]  = "æ"; entities[70]  = "æ";
  chars[71]  = "ç"; entities[71]  = "ç";
  chars[72]  = "è"; entities[72]  = "è";
  chars[73]  = "é"; entities[73]  = "é";
  chars[74]  = "ê"; entities[74]  = "ê";
  chars[75]  = "ë"; entities[75]  = "ë";
  chars[76]  = "ì"; entities[76]  = "ì";
  chars[77]  = "í"; entities[77]  = "í";
  chars[78]  = "î"; entities[78]  = "î";
  chars[79]  = "ï"; entities[79]  = "ï";
  chars[80]  = "ð"; entities[80]  = "ð";
  chars[81]  = "ñ"; entities[81]  = "ñ";
  chars[82]  = "ò"; entities[82]  = "ò";
  chars[83]  = "ó"; entities[83]  = "ó";
  chars[84]  = "ô"; entities[84]  = "ô";
  chars[85]  = "õ"; entities[85]  = "õ";
  chars[86]  = "ö"; entities[86]  = "ö";
  chars[87]  = "÷"; entities[87]  = "÷";
  chars[88]  = "ø"; entities[88]  = "ø";
  chars[89]  = "ù"; entities[89]  = "ù";
  chars[90]  = "ú"; entities[90]  = "ú";
  chars[91]  = "û"; entities[91]  = "û";
  chars[92]  = "ü"; entities[92]  = "ü";
  chars[93]  = "ý"; entities[93]  = "ý";
  chars[94]  = "þ"; entities[94]  = "þ";
  chars[95]  = "ÿ"; entities[95]  = "ÿ";
  chars[96]  = "Œ"; entities[96]  = "Œ";
  chars[97]  = "œ"; entities[97]  = "œ";
  chars[98]  = "Š"; entities[98]  = "Š";
  chars[99]  = "š"; entities[99]  = "š";
  chars[100] = "Ÿ"; entities[100] = "Ÿ";
  chars[101] = "ƒ"; entities[101] = "ƒ";
  chars[102] = "ˆ"; entities[102] = "ˆ";
  chars[103] = "˜"; entities[103] = "˜";
  chars[104] = "Α"; entities[104] = "Α";
  chars[105] = "Β"; entities[105] = "Β";
  chars[106] = "Γ"; entities[106] = "Γ";
  chars[107] = "Δ"; entities[107] = "Δ";
  chars[108] = "Ε"; entities[108] = "Ε";
  chars[109] = "Ζ"; entities[109] = "Ζ";
  chars[110] = "Η"; entities[110] = "Η";
  chars[111] = "Θ"; entities[111] = "Θ";
  chars[112] = "Ι"; entities[112] = "Ι"
  chars[113] = "Κ"; entities[113] = "Κ";
  chars[114] = "Λ"; entities[114] = "Λ";
  chars[115] = "Μ"; entities[115] = "Μ";
  chars[116] = "Ν"; entities[116] = "Ν";
  chars[117] = "Ξ"; entities[117] = "Ξ";
  chars[118] = "Ο"; entities[118] = "Ο";
  chars[119] = "Π"; entities[119] = "Π";
  chars[120] = "Ρ"; entities[120] = "Ρ";
  chars[121] = "Σ"; entities[121] = "Σ";
  chars[122] = "Τ"; entities[122] = "Τ";
  chars[123] = "Υ"; entities[123] = "Υ";
  chars[124] = "Φ"; entities[124] = "Φ";
  chars[125] = "Χ"; entities[125] = "Χ";
  chars[126] = "Ψ"; entities[126] = "Ψ";
  chars[127] = "Ω"; entities[127] = "Ω";
  chars[128] = "α"; entities[128] = "α";
  chars[129] = "β"; entities[129] = "β";
  chars[130] = "γ"; entities[130] = "γ";
  chars[131] = "δ"; entities[131] = "δ";
  chars[132] = "ε"; entities[132] = "ε";
  chars[133] = "ζ"; entities[133] = "ζ";
  chars[134] = "η"; entities[134] = "η";
  chars[135] = "θ"; entities[135] = "θ";
  chars[136] = "ι"; entities[136] = "ι";
  chars[137] = "κ"; entities[137] = "κ";
  chars[138] = "λ"; entities[138] = "λ";
  chars[139] = "μ"; entities[139] = "μ";
  chars[140] = "ν"; entities[140] = "ν";
  chars[141] = "ξ"; entities[141] = "ξ";
  chars[142] = "ο"; entities[142] = "ο";
  chars[143] = "π"; entities[143] = "π";
  chars[144] = "ρ"; entities[144] = "ρ";
  chars[145] = "ς"; entities[145] = "ς";
  chars[146] = "σ"; entities[146] = "σ";
  chars[147] = "τ"; entities[147] = "τ";
  chars[148] = "υ"; entities[148] = "υ";
  chars[149] = "φ"; entities[149] = "φ";
  chars[150] = "χ"; entities[150] = "χ";
  chars[151] = "ψ"; entities[151] = "ψ";
  chars[152] = "ω"; entities[152] = "ω";
  chars[153] = "ϑ"; entities[153] = "ϑ";
  chars[154] = "ϒ"; entities[154] = "ϒ";
  chars[155] = "ϖ"; entities[155] = "ϖ";
  chars[156] = "–"; entities[156] = "–";
  chars[157] = "—"; entities[157] = "—";
  chars[158] = "‘"; entities[158] = "‘";
  chars[159] = "’"; entities[159] = "’";
  chars[160] = "‚"; entities[160] = "‚";
  chars[161] = "“"; entities[161] = "“";
  chars[162] = "”"; entities[162] = "”";
  chars[163] = "„"; entities[163] = "„";
  chars[164] = "†"; entities[164] = "†";
  chars[165] = "‡"; entities[165] = "‡";
  chars[166] = "•"; entities[166] = "•";
  chars[167] = "…"; entities[167] = "…";
  chars[168] = "‰"; entities[168] = "‰";
  chars[169] = "′"; entities[169] = "′";
  chars[170] = "″"; entities[170] = "″";
  chars[171] = "‹"; entities[171] = "‹";
  chars[172] = "›"; entities[172] = "›";
  chars[173] = "‾"; entities[173] = "‾";
  chars[174] = "⁄"; entities[174] = "⁄";
  chars[175] = "€"; entities[175] = "€";
  chars[176] = "™"; entities[176] = "™";
  chars[177] = "←"; entities[177] = "←";
  chars[178] = "↑"; entities[178] = "↑";
  chars[179] = "→"; entities[179] = "→";
  chars[180] = "↓"; entities[180] = "↓";
  chars[181] = "↔"; entities[181] = "↔";
  chars[182] = "∂"; entities[182] = "∂";
  chars[183] = "∏"; entities[183] = "∏";
  chars[184] = "∑"; entities[184] = "∑";
  chars[185] = "−"; entities[185] = "−";
  chars[186] = "√"; entities[186] = "√";
  chars[187] = "∞"; entities[187] = "∞";
  chars[188] = "∩"; entities[188] = "∩";
  chars[189] = "∫"; entities[189] = "∫";
  chars[190] = "≈"; entities[190] = "≈";
  chars[191] = "≠"; entities[191] = "≠";
  chars[192] = "≡"; entities[192] = "≡";
  chars[193] = "≤"; entities[193] = "≤";
  chars[194] = "≥"; entities[194] = "≥";
  chars[195] = "◊"; entities[195] = "◊";
  chars[196] = "♠"; entities[196] = "♠";
  chars[197] = "♣"; entities[197] = "♣";
  chars[198] = "♥"; entities[198] = "♥";
  chars[199] = "♦"; entities[199] = "♦";
}
  
function replaceCharacters()
{  
  Document.BeginUpdate();
  r.Enabled = false;
  c.Enabled = false;
  
  var s = Document.SelText;
  if (s == "") {
    Document.SelectAll;
    s = Document.SelText;
  }
  for (var i = 0; i < Length(chars); i++) {
    if (ScriptUtils.Pos(chars[i], s) > 0) {
    	s = ScriptUtils.StringReplaceAll(s, chars[i], entities[i]);
    }
  }
  Document.SelText = s;
  Document.EndUpdate();
  ShowMessage("Done!");
  f.ModalResult = mrOk;
}

function replaceEntities()
{  
  Document.BeginUpdate();
  r.Enabled = false;
  c.Enabled = false;
  
  var s = Document.SelText;
  if (s == "") {
    Document.SelectAll;
    s = Document.SelText;
  }
  for (var i = 0; i < Length(chars); i++) {
    if (ScriptUtils.Pos(entities[i], s) > 0) {
    	s = ScriptUtils.StringReplaceAll(s, entities[i], chars[i]);
    }
  }
  Document.SelText = s;
  Document.EndUpdate();
  ShowMessage("Done!");
  f.ModalResult = mrOk;
}

function exit()
{
  f.ModalResult = mrOk;
}

// Load special characters and entities to our arrays
fillArrays();

// build up main app window
f = new TForm(nil);
f.Caption = "Replace entities or special characters";
f.BorderStyle = bsDialog;
f.Position = poScreenCenter;
f.Width = 320;
f.Height = 150;

la = new TLabel(f);
la.Parent = f;
la.Name = "lblA";
la.SetBounds(30, 17, 160, 25);
la.Caption = "Entities => Characters";

r = new TButton(f);
r.Parent = f;
r.Name = "btnReplace";
r.SetBounds(200, 15, 95, 25);
r.Caption = "Replace";
r.OnClick = &replaceEntities;

lb = new TLabel(f);
lb.Parent = f;
lb.Name = "lblB";
lb.SetBounds(30, 52, 160, 25);
lb.Caption = "Characters => Entities";

h = new TButton(f);
h.Parent = f;
h.Name = "btnReplaceChars";
h.SetBounds(200, 50, 95, 25);
h.Caption = "Replace";
h.OnClick = &replaceCharacters;

c = new TButton(f);
c.Parent = f;
c.Name = "btnCancel";
c.SetBounds(200, 85, 95, 25);
c.Caption = "Cancel";
c.OnClick = &exit;

// show the main dialog
f.ShowModal;
f.Free;
Attachments
Replace_entities.zip
(3.07 KiB) Downloaded 423 times

davidhs
Posts: 77
Joined: 13 Mar 2010 00:33
Location: Spain

Re: Convert between HTML entities and special characters

Post by davidhs »

This script does not work :( (I installed last version, 8.80)

I have

Code: Select all

!
á
and when replace characters => entities result is

Code: Select all

&i&e&pi;&sigma;ilo&cap;;xcl;
&&al&pi;ha;&al&pi;ha;cu&tau;&e&pi;&sigma;ilo&cap;;;
instead of

Code: Select all

&iexcl;
&aacute;
I modify this function

Code: Select all

function replaceCharacters()
{  
  Document.BeginUpdate();
  r.Enabled = false;
  c.Enabled = false;
  
  var s = Document.SelText;
  if (s == "") {
    Document.SelectAll;
    s = Document.SelText;
  }
  s2 = s; // <----------
  for (var i = 0; i < Length(chars); i++) {
    if (ScriptUtils.Pos(chars[i], s) > 0) {
    	s = ScriptUtils.StringReplaceAll(s, chars[i], entities[i]);
    	s2 += "\n\n" + s; // <----------
    }
  }
  Document.SelText = s2; // <----------
  Document.EndUpdate();
  ShowMessage("Done!");
  f.ModalResult = mrOk;
}
and return this

Code: Select all

¡
á

&iexcl;
á

&iexcl;
&aacute; // Here it should stop, but continues.

&iexcl;
&&alpha;&alpha;cute;

&i&epsilon;xcl;
&&alpha;&alpha;cut&epsilon;;

&i&e&pi;silon;xcl;
&&al&pi;ha;&al&pi;ha;cut&e&pi;silon;;

&i&e&pi;&sigma;ilon;xcl;
&&al&pi;ha;&al&pi;ha;cut&e&pi;&sigma;ilon;;

&i&e&pi;&sigma;ilon;xcl;
&&al&pi;ha;&al&pi;ha;cu&tau;&e&pi;&sigma;ilon;;

&i&e&pi;&sigma;ilo&cap;;xcl;
&&al&pi;ha;&al&pi;ha;cu&tau;&e&pi;&sigma;il

User avatar
Rickard Johansson
Site Admin
Posts: 6001
Joined: 19 Jul 2006 14:29

Re: Convert between HTML entities and special characters

Post by Rickard Johansson »

It's been fixed in the next release. Thanks!

davidhs
Posts: 77
Joined: 13 Mar 2010 00:33
Location: Spain

Re: Convert between HTML entities and special characters

Post by davidhs »

Rickard Johansson wrote:It's been fixed in the next release. Thanks!
On v8.91 works fine, thanks!

Post Reply