HTML Entity Encoder/Decoder

Source: Internet
Author: User
Detail refer to http://andrewu.co.uk/clj/entityencode.

Function Texttoentities (strplaintext, blnpartialencodeonly) {
VaR Strpartial = [];
VaR Strfull = [];
VaR INTP =   0 ;
VaR INTF =   0 ;
VaR Objpartialregexp = ( New Regexp). Compile ( " [\ W \ s] " );

For ( VaR Inti = 0 ; Inti < Strplaintext. length; ++ INTI) {
VaR Strchar = Strplaintext. charat (INTI );
VaR Intchar = Strchar. charcodeat ( 0 );

If (Isnan (intchar )) {
//If char failed to decode, leave as char
Strpartial. Push (strfull. Push (strchar ));
}
Else   {
VaR Strentity =   " &# "   + Intchar +   " ; " ;
Strfull. Push (strentity );
// If char was [a-zA-Z0-9 _ \ t] Leave as char, else Replace with Entity
Strpartial. Push (objpartialregexp. Test (strchar) ? Strchar: strentity );
}
}
Return (Blnpartialencodeonly ? Strpartial. Join ( "" ): Strfull. Join ( "" ));
}

Function Entitiestotext (strencodedtext) {
VaR Strdata = String (strencodedtext );
VaR Objregexp = ( New Regexp). Compile ( " & # (\ D + ); " , " IG " );

/**/ /*For each match to any entity, replace that
Entity globally with its single char equivalent*/
While (Strdata. Match (objregexp )) {
VaR Strcharmatch = Regexp. $ 1 ;
VaR Objregexpmatch =   New Regexp ( " &# "   + Strcharmatch +   " ; " , " IG " );
Strdata = Strdata. Replace (objregexpmatch, String. fromcharcode (strcharmatch ));
}
Return Strdata;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.