Pages

Wednesday, January 19, 2011

Decode Html Using Javascipt - decodeHTML()

The following function helps you replace most of the html equivalents of the special strings like &,"..etc to their original form. I hope it covers all of them, but i am not sure...

And yes there is a more elegant way to do this. But I thought it might be useful for to give an idea to people who are looking out for this.

function decodeHTML(str) {

/*
 *   @Param : str - your native html string to be decoded     *
 *   @Return : str - decoded html                                        *
 *   @Created : 19th Jan 2011                                            *
 */

str=str.replace(/ /gi, " ");
str=str.replace(/–/gi, "–");
str=str.replace(/—/gi, "—");
str=str.replace(/¡/gi, "¡");
str=str.replace(/¿/gi, "¿");
str=str.replace(/"/gi, "\"");
str=str.replace(/“/gi, "“");
str=str.replace(/”/gi, "”");
str=str.replace(/‘/gi, "‘");
str=str.replace(/’/gi, "’");
str=str.replace(/«/gi, "«");
str=str.replace(/»/gi, "»");
str=str.replace(/&/gi, "&");
str=str.replace(/¢/gi, "¢");
str=str.replace(/©/gi, "©");
str=str.replace(/÷/gi, "÷");
str=str.replace(/>/gi, ">");
str=str.replace(/&lt;/gi, "<");
str=str.replace(/&micro;/gi, "µ");
str=str.replace(/&middot;/gi, "·");
str=str.replace(/&para;/gi, "¶");
str=str.replace(/&plusmn;/gi, "±");
str=str.replace(/&euro;/gi, "€");
str=str.replace(/&pound;/gi, "£");
str=str.replace(/&reg;/gi, "®");
str=str.replace(/&sect;/gi, "§");
str=str.replace(/&trade;/gi, "™");
str=str.replace(/&yen;/gi, "¥");
str=str.replace(/&aacute;/gi, "á");
str=str.replace(/&Aacute;/gi, "Á");
str=str.replace(/&agrave;/gi, "à");
str=str.replace(/&Agrave;/gi, "À");
str=str.replace(/&acirc;/gi, "â");
str=str.replace(/&Acirc;/gi, "Â");
str=str.replace(/&aring;/gi, "å");
str=str.replace(/&Aring;/gi, "Å");
str=str.replace(/&atilde;/gi, "ã");
str=str.replace(/&Atilde;/gi, "Ã");
str=str.replace(/&auml;/gi, "ä");
str=str.replace(/&Auml;/gi, "Ä");
str=str.replace(/&aelig;/gi, "æ");
str=str.replace(/&AElig;/gi, "Æ");
str=str.replace(/&ccedil;/gi, "ç");
str=str.replace(/&Ccedil;/gi, "Ç");
str=str.replace(/&eacute;/gi, "é");
str=str.replace(/&Eacute;/gi, "É");
str=str.replace(/&egrave;/gi, "è");
str=str.replace(/&Egrave;/gi, "È");
str=str.replace(/&ecirc;/gi, "ê");
str=str.replace(/&Ecirc;/gi, "Ê");
str=str.replace(/&euml;/gi, "ë");
str=str.replace(/&Euml;/gi, "Ë");
str=str.replace(/&iacute;/gi, "í");
str=str.replace(/&Iacute;/gi, "Í");
str=str.replace(/&igrave;/gi, "ì");
str=str.replace(/&Igrave;/gi, "Ì");
str=str.replace(/&icirc;/gi, "î");
str=str.replace(/&Icirc;/gi, "Î");
str=str.replace(/&iuml;/gi, "ï");
str=str.replace(/&Iuml;/gi, "Ï");
str=str.replace(/&ntilde;/gi, "ñ");
str=str.replace(/&Ntilde;/gi, "Ñ");
str=str.replace(/&oacute;/gi, "ó");
str=str.replace(/&Oacute;/gi, "Ó");
str=str.replace(/&ograve;/gi, "ò");
str=str.replace(/&Ograve;/gi, "Ò");
str=str.replace(/&ocirc;/gi, "ô");
str=str.replace(/&Ocirc;/gi, "Ô");
str=str.replace(/&oslash;/gi, "ø");
str=str.replace(/&Oslash;/gi, "Ø");
str=str.replace(/&otilde;/gi, "õ");
str=str.replace(/&Otilde;/gi, "Õ");
str=str.replace(/&ouml;/gi, "ö");
str=str.replace(/&Ouml;/gi, "Ö");
str=str.replace(/&szlig;/gi, "ß");
str=str.replace(/&uacute;/gi, "ú");
str=str.replace(/&Uacute;/gi, "Ú");
str=str.replace(/&ugrave;/gi, "ù");
str=str.replace(/&Ugrave;/gi, "Ù");
str=str.replace(/&ucirc;/gi, "û");
str=str.replace(/&Ucirc;/gi, "Û");
str=str.replace(/&uuml;/gi, "ü");
str=str.replace(/&Uuml;/gi, "Ü");
str=str.replace(/&yuml;/gi, "ÿ");


return str;
}

Hope this helps :)

2 comments: