PHP code conversion function example

Source: Internet
Author: User
In PHP, mb_convert_encoding is a PHP code conversion function that can be used to convert multi-byte string encoding. The following is a detailed introduction. There are a large number of functions in PHP to support our application. The following describes the PHP encoding and conversion functions. Hope to help you. The mb_convert_encoding function is a multi-byte character string conversion function in PHP.

Code conversion can be performed easily when necessary (for example, to solve the problem of text garbled characters generated by Ajax in GB2312 encoding environment) to solve the problem of webpage garbled characters, which is very convenient to use, very efficient. almost all codes are supported. PHP 4> = 4.0.6 and PHP 5.

PHP code conversion 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 the encoding of the original string.
For example, if JIS, eucjp-win, and sjis-win are both specified
* @ Return string
String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding])

Example of PHP code conversion function:

1. convert GBK encoding string into UTF-8 encoding string

<? PHP header ("content-Type: text/html; charset = Utf-8"); echo mb_convert_encoding ("You are my friend", "UTF-8", "GBK");?>

2, the UTF-8 encoding string into GB2312 encoding string

// Remember to save the file into a UTF-8 encoded file before testing <? PHP 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. In this way, the character sets other than the first 128 characters (display characters) are expressed by NCR (Numeric character reference, for example, "Chinese characters" will be converted to "Chinese 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 your website's original code mb_http_output ("HTML-ENTITIES"); ob_start ('MB _ output_handler ');

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

If the PHP mbstring extension is not enabled, you need to make the following settings to allow PHP to support the extension.

1. windows Server environment

Edit the PHP. ini file, and replace the file before extension = PHP_mbstring.dll. remove the file and restart the web server.

2. Linux server environment

Add the -- enable-mbstring = cn compilation parameter during compilation configuration, and then compile and install PHP.

Conclusion:

If you have any questions about using PHP encoding and conversion functions, refer to the PHP Manual.

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.