A very quick article to document a HTML Entity decoder in Zoho Creator.
Why?
Sometimes when receiving data from a third-party, we may receive some strings containing "&" or " " and obviously want to display these as decoded HTML entities. (Zoho if you're listening) Ideally, Zoho may add this to their zoho.encryption namespace such as zoho.encryption.htmldecode(string). But at time of print, it doesn't exist and in the meantime, I've made a function that does this.
How?
Not sure how else to do this but I simply wrote a function in Zoho Creator for each instance that I knew a HTML entity would be submitted. So it doesn't decode every HTML in existence, only the ones I know will happen.
Here's the Zoho Deluge code I use (note that I've put this in a namespace called "DataCleansing", but you can call it whatever you like):
string DataCleansing.fn_DecodeHtmlEntities( string p_StringToDecode ) { // initialize v_StringToDecode = ifnull(p_StringToDecode,""); // // map string replacements m_HtmlEntity = Map(); m_HtmlEntity.put("&","&"); m_HtmlEntity.put(" "," "); // // loop through each of the above replacing where found for each v_HtmlKey in m_HtmlEntity.keys() { v_StringToDecode = v_StringToDecode.replaceAll(v_HtmlKey, m_HtmlEntity.get(v_HtmlKey), true); } // // output return v_StringToDecode; }
- string DataCleansing.fn_DecodeHtmlEntities( string p_StringToDecode )
- {
- // initialize
- v_StringToDecode = ifnull(p_StringToDecode,"");
- //
- // map string replacements
- m_HtmlEntity = Map();
- m_HtmlEntity.put("&","&");
- m_HtmlEntity.put(" "," ");
- //
- // loop through each of the above replacing where found
- for each v_HtmlKey in m_HtmlEntity.keys()
- {
- v_StringToDecode = v_StringToDecode.replaceAll(v_HtmlKey, m_HtmlEntity.get(v_HtmlKey), true);
- }
- //
- // output
- return v_StringToDecode;
- }
Usage:
v_TestString = "Father & Sons"; v_DecodedString = thisapp.DataCleansing.fn_DecodeHtmlEntities(v_TestString); info v_DecodedString; // yields: Father & Sons
- v_TestString = "Father & Sons";
- v_DecodedString = thisapp.DataCleansing.fn_DecodeHtmlEntities(v_TestString);
- info v_DecodedString;
- // yields: Father & Sons
A more comprehensive list
v_HtmlEntityNames = "amp,lt,gt,nbsp,iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,not,shy,reg,macr,deg,plusmn,sup2,sup3,acute,micro,para,middot,cedil,sup1,ordm,raquo,frac14,frac12,frac34,iquest,Agrave,Aacute,Acirc,Atilde,Auml,Aring,AElig,Ccedil,Egrave,Eacute,Ecirc,Euml,Igrave,Iacute,Icirc,Iuml,ETH,Ntilde,Ograve,Oacute,Ocirc,Otilde,Ouml,times,Oslash,Ugrave,Uacute,Ucirc,Uuml,Yacute,THORN,szlig,agrave,aacute,acirc,atilde,auml,aring,aelig,ccedil,egrave,eacute,ecirc,euml,igrave,iacute,icirc,iuml,eth,ntilde,ograve,oacute,ocirc,otilde,ouml,divide,oslash,ugrave,uacute,ucirc,uuml,yacute,thorn,yuml,OElig,oelig,Scaron,scaron,Yuml,fnof,circ,tilde,Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,Mu,Nu,Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,Omega,alpha,beta,gamma,delta,epsilon,zeta,eta,theta,iota,kappa,lambda,mu,nu,xi,omicron,pi,rho,sigmaf,sigma,tau,upsilon,phi,chi,psi,omega,thetasym,upsih,piv,ensp,emsp,thinsp,zwnj,zwj,lrm,rlm,ndash,mdash,lsquo,rsquo,sbquo,ldquo,rdquo,bdquo,dagger,Dagger,bull,hellip,permil,prime,Prime,lsaquo,rsaquo,oline,frasl,euro,image,weierp,real,trade,alefsym,larr,uarr,rarr,darr,harr,crarr,lArr,uArr,rArr,dArr,hArr,forall,part,exist,empty,nabla,isin,notin,ni,prod,sum,minus,lowast,radic,prop,infin,ang,and,or,cap,cup,int,there4,sim,cong,asymp,ne,equiv,le,ge,sub,sup,nsub,sube,supe,oplus,otimes,perp,sdot,lceil,rceil,lfloor,rfloor,lang,rang,loz,spades,clubs,hearts,diams"; v_HtmlEntityChars = "&,<,>, ,¡,¢,£,¤,¥,¦,§,¨,©,ª,«,¬,,®,¯,°,±,²,³,´,µ,¶,·,¸,¹,º,»,¼,½,¾,¿,À,Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,×,Ø,Ù,Ú,Û,Ü,Ý,Þ,ß,à,á,â,ã,ä,å,æ,ç,è,é,ê,ë,ì,í,î,ï,ð,ñ,ò,ó,ô,õ,ö,÷,ø,ù,ú,û,ü,ý,þ,ÿ,Œ,œ,Š,š,Ÿ,ƒ,ˆ,˜,Α,Β,Γ,Δ,Ε,Ζ,Η,Θ,Ι,Κ,Λ,Μ,Ν,Ξ,Ο,Π,Ρ,Σ,Τ,Υ,Φ,Χ,Ψ,Ω,α,β,γ,δ,ε,ζ,η,θ,ι,κ,λ,μ,ν,ξ,ο,π,ρ,ς,σ,τ,υ,φ,χ,ψ,ω,ϑ,ϒ,ϖ, , , ,,,,,–,—,‘,’,‚,“,”,„,†,‡,•,…,‰,′,″,‹,›,‾,⁄,€,ℑ,℘,ℜ,™,ℵ,←,↑,→,↓,↔,↵,⇐,⇑,⇒,⇓,⇔,∀,∂,∃,∅,∇,∈,∉,∋,∏,∑,−,∗,√,∝,∞,∠,∧,∨,∩,∪,∫,∴,∼,≅,≈,≠,≡,≤,≥,⊂,⊃,⊄,⊆,⊇,⊕,⊗,⊥,⋅,⌈,⌉,⌊,⌋,〈,〉,◊,♠,♣,♥,♦"; l_HtmlEntityNames = v_HtmlEntityNames.toList(); l_HtmlEntityChars = v_HtmlEntityChars.toList(); // // add these exceptions (quotes) which wouldn't convert to a list l_HtmlEntityNames.add("quo"); l_HtmlEntityNames.add("apos"); l_HtmlEntityChars.add("\""); l_HtmlEntityChars.add("'"); // // build a map m_HtmlEntity = Map(); for each index v_EntityIndex in l_HtmlEntityNames { m_HtmlEntity.put("&" + l_HtmlEntityNames.get(v_EntityIndex) + ";", l_HtmlEntityChars.get(v_EntityIndex)); } // // loop through list of entities to replace each one in our string for each v_HtmlKey in m_HtmlEntity.keys() { v_Test = v_Test.replaceAll(v_HtmlKey, m_HtmlEntity.get(v_HtmlKey), true); } info v_Test; // yields: Father & Sons
- v_HtmlEntityNames = "amp,lt,gt,nbsp,iexcl,cent,pound,curren,yen,brvbar,sect,uml,copy,ordf,laquo,not,shy,reg,macr,deg,plusmn,sup2,sup3,acute,micro,para,middot,cedil,sup1,ordm,raquo,frac14,frac12,frac34,iquest,Agrave,Aacute,Acirc,Atilde,Auml,Aring,AElig,Ccedil,Egrave,Eacute,Ecirc,Euml,Igrave,Iacute,Icirc,Iuml,ETH,Ntilde,Ograve,Oacute,Ocirc,Otilde,Ouml,times,Oslash,Ugrave,Uacute,Ucirc,Uuml,Yacute,THORN,szlig,agrave,aacute,acirc,atilde,auml,aring,aelig,ccedil,egrave,eacute,ecirc,euml,igrave,iacute,icirc,iuml,eth,ntilde,ograve,oacute,ocirc,otilde,ouml,divide,oslash,ugrave,uacute,ucirc,uuml,yacute,thorn,yuml,OElig,oelig,Scaron,scaron,Yuml,fnof,circ,tilde,Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,Mu,Nu,Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,Omega,alpha,beta,gamma,delta,epsilon,zeta,eta,theta,iota,kappa,lambda,mu,nu,xi,omicron,pi,rho,sigmaf,sigma,tau,upsilon,phi,chi,psi,omega,thetasym,upsih,piv,ensp,emsp,thinsp,zwnj,zwj,lrm,rlm,ndash,mdash,lsquo,rsquo,sbquo,ldquo,rdquo,bdquo,dagger,Dagger,bull,hellip,permil,prime,Prime,lsaquo,rsaquo,oline,frasl,euro,image,weierp,real,trade,alefsym,larr,uarr,rarr,darr,harr,crarr,lArr,uArr,rArr,dArr,hArr,forall,part,exist,empty,nabla,isin,notin,ni,prod,sum,minus,lowast,radic,prop,infin,ang,and,or,cap,cup,int,there4,sim,cong,asymp,ne,equiv,le,ge,sub,sup,nsub,sube,supe,oplus,otimes,perp,sdot,lceil,rceil,lfloor,rfloor,lang,rang,loz,spades,clubs,hearts,diams";
- v_HtmlEntityChars = "&,<,>, ,¡,¢,£,¤,¥,¦,§,¨,©,ª,«,¬,,®,¯,°,±,²,³,´,µ,¶,·,¸,¹,º,»,¼,½,¾,¿,À,Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,×,Ø,Ù,Ú,Û,Ü,Ý,Þ,ß,à,á,â,ã,ä,å,æ,ç,è,é,ê,ë,ì,í,î,ï,ð,ñ,ò,ó,ô,õ,ö,÷,ø,ù,ú,û,ü,ý,þ,ÿ,Œ,œ,Š,š,Ÿ,ƒ,ˆ,˜,Α,Β,Γ,Δ,Ε,Ζ,Η,Θ,Ι,Κ,Λ,Μ,Ν,Ξ,Ο,Π,Ρ,Σ,Τ,Υ,Φ,Χ,Ψ,Ω,α,β,γ,δ,ε,ζ,η,θ,ι,κ,λ,μ,ν,ξ,ο,π,ρ,ς,σ,τ,υ,φ,χ,ψ,ω,ϑ,ϒ,ϖ, , , ,,,,,–,—,‘,’,‚,“,”,„,†,‡,•,…,‰,′,″,‹,›,‾,⁄,€,ℑ,℘,ℜ,™,ℵ,←,↑,→,↓,↔,↵,⇐,⇑,⇒,⇓,⇔,∀,∂,∃,∅,∇,∈,∉,∋,∏,∑,−,∗,√,∝,∞,∠,∧,∨,∩,∪,∫,∴,∼,≅,≈,≠,≡,≤,≥,⊂,⊃,⊄,⊆,⊇,⊕,⊗,⊥,⋅,⌈,⌉,⌊,⌋,〈,〉,◊,♠,♣,♥,♦";
- l_HtmlEntityNames = v_HtmlEntityNames.toList();
- l_HtmlEntityChars = v_HtmlEntityChars.toList();
- //
- // add these exceptions (quotes) which wouldn't convert to a list
- l_HtmlEntityNames.add("quo");
- l_HtmlEntityNames.add("apos");
- l_HtmlEntityChars.add("\"");
- l_HtmlEntityChars.add("'");
- //
- // build a map
- m_HtmlEntity = Map();
- for each index v_EntityIndex in l_HtmlEntityNames
- {
- m_HtmlEntity.put("&" + l_HtmlEntityNames.get(v_EntityIndex) + ";", l_HtmlEntityChars.get(v_EntityIndex));
- }
- //
- // loop through list of entities to replace each one in our string
- for each v_HtmlKey in m_HtmlEntity.keys()
- {
- v_Test = v_Test.replaceAll(v_HtmlKey, m_HtmlEntity.get(v_HtmlKey), true);
- }
- info v_Test;
- // yields: Father & Sons