Introduction to the use of the php internal string encoding conversion function mb_convert_encoding

Source: Internet
Author: User
Introduction to the use of the mb_convert_encoding function in the php internal character code conversion function in http://hi.baidu.com/get52/blog/item/56f4d80336bf8ee208fa9307.htmlmb_convert_encod
Http://hi.baidu.com/get52/blog/item/56f4d80336bf8ee208fa9307.html


The mb_convert_encoding function is a multi-byte character string conversion function in php. it can be used for encoding when necessary. for example, it solves the problem of Chinese character garbled characters generated by Ajax in the GB2312 encoding environment. Supports almost all encodings. PHP 4> = 4.0.6 and PHP 5 are supported.
Function prototype:
/**
* Multi-byte string encoding conversion function
*
* @ Param string str the string to be encoded and converted
* @ Param string to_encoding specifies the conversion to a certain encoding, such as gb2312, gbk, and UTF-8.
* @ Param mixed from_encoding: specify both the encoding of the original string, for example, JIS, eucjp-win, and sjis-win.
* @ Return string
String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding])

Example:
1. convert GBK encoding string into UTF-8 encoding string
Header ("content-Type: text/html; charset = Utf-8 ");
Echo mb_convert_encoding ("You are my good friend", "UTF-8", "GBK ");
?>
2, the UTF-8 encoding string into GB2312 encoding string
// Remember to save the file to a UTF-8 encoded file before testing
Header ("content-Type: text/html; charset = gb2312 ");
Echo mb_convert_encoding ("You are my good friend", "gb312", "UTF-8 ");
?>

3. convert the entire page

This method applies to all encoding environments. Use the NCR (Numeric character reference, for example, "Chinese character" will be converted to "Chinese character") to represent character sets other than the first 128 characters, such encoding can be normally displayed on the page in any encoding environment.

Add the following three lines of code to the header of the PHP file:
Mb_internal_encoding ("gb2312"); // Here, gb2312 is the original code of your website.
Mb_http_output ("HTML-ENTITIES ");
Ob_start ('MB _ output_handler ');

However, to use the mb_convert_encoding function, you must enable the mbstring (multi-byte string) extension of PHP.

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.