Php converts a string to an HTML object reference class _ PHP Tutorial

Source: Internet
Author: User
Php converts a string into a class referenced by an HTML object. Copy the code as follows: classHtmlEncode {static $ _ convertToHtmlEntitiesSrcEncodingUTF-8; *** convert a non-ASCII string to an HTML entity *** @ exampleHtmlEncode: encode (I believe The code is as follows:


Class HtmlEncode {
Static $ _ convertToHtmlEntitiesSrcEncoding = 'utf-8 ';

/**
* Convert a non-ASCII string to an HTML object
*
* @ Example HtmlEncode: encode ("I Believe"); // output: I believe
* @ Param string $ s the string to be encoded
* @ Return string returns HTML object reference
*/
Public static function encode ($ s, $ srcEncoding = 'utf-8 '){
Self: $ _ convertToHtmlEntitiesSrcEncoding = $ srcEncoding;
Return preg_replace_callback ('| [^ \ x00-\ x7F] + |', array (_ CLASS __, '_ convertToHtmlEntities'), $ s );
}

Public static function _ convertToHtmlEntities ($ data ){
If (is_array ($ data )){
$ Chars = str_split (iconv (self ::$ _ convertToHtmlEntitiesSrcEncoding, "UCS-2BE", $ data [0]), 2 );
$ Chars = array_map (array (_ CLASS __,__ FUNCTION _), $ chars );
Return join ("", $ chars );
} Else {
$ Code = hexdec (sprintf ("% 02 s % 02 s;", dechex (ord ($ data {0 })), dechex (ord ($ data {1 }))));
Return sprintf ("& # % s;", $ code );
}
}
}

The http://www.bkjia.com/PHPjc/326471.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326471.htmlTechArticle code is as follows: class HtmlEncode {static $ _ convertToHtmlEntitiesSrcEncoding = 'utf-8';/*** converts a non-ASCII string to an HTML entity ** @ example HtmlEncode :: encode ("I believe...

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.