Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q243 How can I replace HTML entities with the appropriate characters?

You are here: irt.org | FAQ | JavaScript | Text | Q243 [ previous next ]

<FORM NAME="test">
<INPUT TYPE="TEXT" NAME="output" SIZE="60">
<INPUT TYPE="BUTTON" VALUE="Convert" onclick="doit()">
</FORM>

<SCRIPT LANGUAGE="JavaScript"><!--
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function doit() {
    text = document.test.output.value;

    text = replace(text,'"',unescape('%22'));
    text = replace(text,'&',unescape('%26'));
    text = replace(text,'<',unescape('%3C'));
    text = replace(text,'>',unescape('%3E'));
    text = replace(text,' ',unescape('%A0'));
    text = replace(text,'¡',unescape('%A1'));
    text = replace(text,'¢',unescape('%A2'));
    text = replace(text,'£',unescape('%A3'));
    text = replace(text,'¥',unescape('%A5'));
    text = replace(text,'¦',unescape('%A6'));
    text = replace(text,'§',unescape('%A7'));
    text = replace(text,'¨',unescape('%A8'));
    text = replace(text,'©',unescape('%A9'));
    text = replace(text,'ª',unescape('%AA'));
    text = replace(text,'«',unescape('%AB'));
    text = replace(text,'¬',unescape('%AC'));
    text = replace(text,'­',unescape('%AD'));
    text = replace(text,'®',unescape('%AE'));
    text = replace(text,'¯',unescape('%AF'));
    text = replace(text,'°',unescape('%B0'));
    text = replace(text,'±',unescape('%B1'));
    text = replace(text,'²',unescape('%B2'));
    text = replace(text,'³',unescape('%B3'));
    text = replace(text,'´',unescape('%B4'));
    text = replace(text,'µ',unescape('%B5'));
    text = replace(text,'¶',unescape('%B6'));
    text = replace(text,'·',unescape('%B7'));
    text = replace(text,'¸',unescape('%B8'));
    text = replace(text,'¹',unescape('%B9'));
    text = replace(text,'º',unescape('%BA'));
    text = replace(text,'»',unescape('%BB'));
    text = replace(text,'¼',unescape('%BC'));
    text = replace(text,'½',unescape('%BD'));
    text = replace(text,'¾',unescape('%BE'));
    text = replace(text,'¿',unescape('%BF'));
    text = replace(text,'À',unescape('%C0'));
    text = replace(text,'Á',unescape('%C1'));
    text = replace(text,'Â',unescape('%C2'));
    text = replace(text,'Ã',unescape('%C3'));
    text = replace(text,'Ä',unescape('%C4'));
    text = replace(text,'Å',unescape('%C5'));
    text = replace(text,'Æ',unescape('%C6'));
    text = replace(text,'Ç',unescape('%C7'));
    text = replace(text,'È',unescape('%C8'));
    text = replace(text,'É',unescape('%C9'));
    text = replace(text,'Ê',unescape('%CA'));
    text = replace(text,'Ë',unescape('%CB'));
    text = replace(text,'Ì',unescape('%CC'));
    text = replace(text,'Í',unescape('%CD'));
    text = replace(text,'Î',unescape('%CE'));
    text = replace(text,'Ï',unescape('%CF'));
    text = replace(text,'Ð',unescape('%D0'));
    text = replace(text,'Ñ',unescape('%D1'));
    text = replace(text,'Ò',unescape('%D2'));
    text = replace(text,'Ó',unescape('%D3'));
    text = replace(text,'Ô',unescape('%D4'));
    text = replace(text,'Õ',unescape('%D5'));
    text = replace(text,'Ö',unescape('%D6'));
    text = replace(text,'×',unescape('%D7'));
    text = replace(text,'Ø',unescape('%D8'));
    text = replace(text,'Ù',unescape('%D9'));
    text = replace(text,'Ú',unescape('%DA'));
    text = replace(text,'Û',unescape('%DB'));
    text = replace(text,'Ü',unescape('%DC'));
    text = replace(text,'Ý',unescape('%DD'));
    text = replace(text,'Þ',unescape('%DE'));
    text = replace(text,'ß',unescape('%DF'));
    text = replace(text,'à',unescape('%E0'));
    text = replace(text,'á',unescape('%E1'));
    text = replace(text,'â',unescape('%E2'));
    text = replace(text,'ã',unescape('%E3'));
    text = replace(text,'ä',unescape('%E4'));
    text = replace(text,'å',unescape('%E5'));
    text = replace(text,'æ',unescape('%E6'));
    text = replace(text,'ç',unescape('%E7'));
    text = replace(text,'è',unescape('%E8'));
    text = replace(text,'é',unescape('%E9'));
    text = replace(text,'ê',unescape('%EA'));
    text = replace(text,'ë',unescape('%EB'));
    text = replace(text,'ì',unescape('%EC'));
    text = replace(text,'í',unescape('%ED'));
    text = replace(text,'î',unescape('%EE'));
    text = replace(text,'ï',unescape('%EF'));
    text = replace(text,'ð',unescape('%F0'));
    text = replace(text,'ñ',unescape('%F1'));
    text = replace(text,'ò',unescape('%F2'));
    text = replace(text,'ó',unescape('%F3'));
    text = replace(text,'ô',unescape('%F4'));
    text = replace(text,'õ',unescape('%F5'));
    text = replace(text,'ö',unescape('%F6'));
    text = replace(text,'÷',unescape('%F7'));
    text = replace(text,'ø',unescape('%F8'));
    text = replace(text,'ù',unescape('%F9'));
    text = replace(text,'ú',unescape('%FA'));
    text = replace(text,'û',unescape('%FB'));
    text = replace(text,'ü',unescape('%FC'));
    text = replace(text,'ý',unescape('%FD'));
    text = replace(text,'þ',unescape('%FE'));
    text = replace(text,'ÿ',unescape('%FF'));

    document.test.output.value = text;
}
//--></SCRIPT>

Feedback on 'Q243 How can I replace HTML entities with the appropriate characters?'

©2018 Martin Webb