PHP internal string encoding conversion function Mb_convert_encoding use method introduced
Source: Internet
Author: User
Introduction to PHP internal string encoding conversion function Mb_convert_encoding usage method
Http://hi.baidu.com/get52/blog/item/56f4d80336bf8ee208fa9307.html
mb_convert_encoding function for PHP internal Multi-byte string encoding conversion function, can be used in the case of code conversion, such as: To solve the GB2312 encoding environment using AJAX generated Chinese characters garbled problem. Support for almost all encodings, version support PHP 4 >= 4.0.6, PHP 5.
Function Prototypes:
/**
* Multi-byte string encoding conversion function
*
* @param string str needs to encode the converted strings
* @param string to_encoding specified to a certain encoding, such as: gb2312, GBK, utf-8, etc.
* @param mixed From_encoding Mix to specify the encoding of the original string, such as: Specify JIS, Eucjp-win, Sjis-win mixed code
* @return String
String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding])
Examples of Use:
1. Convert GBK encoded string into UTF-8 encoded string
Header ("content-type:text/html; Charset=utf-8 ");
Echo mb_convert_encoding ("You Are my good friend", "UTF-8", "GBK");
?>
2. Convert UTF-8 encoded string into GB2312 encoded string
Note Save this file as a UTF-8 encoded format file for re-testing
Header ("content-type:text/html; charset=gb2312 ");
Echo mb_convert_encoding ("You Are my good friend", "gb312", "utf-8");
?>
3, the entire page to convert
This method applies to all coding environments. The character set other than the first 128 characters (display character) is used NCR (Numeric character reference, such as "kanji" will be converted to "& #27721;& #23383;" This form) to indicate that the code in any encoding environment, the page can be displayed normally.
The method is to add the following three lines of code to the header of the PHP file:
Mb_internal_encoding ("gb2312"); The gb2312 here is the original code of your website.
Mb_http_output ("Html-entities");
Ob_start (' Mb_output_handler ');
However, using the Mb_convert_encoding function requires PHP mbstring (multi-byte String) extension to be enabled.
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