There are a large number of functions in PHP that support our requirements application. The function described below is the PHP encoding conversion function. Hope to help you. The mb_convert_encoding function is the PHP internal multibyte string encoding conversion function.
Can be in need of use occasions (such as: Solve in the GB2312 coding environment using AJAX generated in the text garbled problem) to facilitate the code conversion to solve the problem of garbled Web pages, the use of very convenient, very high efficiency, almost support all coding. PHP 4 >= 4.0.6, PHP 5 version support.
PHP Code conversion function prototype:
/**
* Multi-byte string encoding conversion function
*
* @param string STR requires a coded conversion
* @param string to_encoding Specifies conversion to some encoding
, such as: gb2312, GBK, utf-8, etc.
* @param mixed from_encoding to mix and specify the encoding of the original string
, such as: also specify JIS, Eucjp-win, Sjis-win mixed code
* @return String
String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding])
PHP Code Conversion function Use Example:
1. Convert GBK encoded string into UTF-8 coded string
<? PHP
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 coded string
Note Save this file as a UTF-8
encoded format file and then test the
<? PHP
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 is applicable to all coding environments. In this way, the character set of the first 128 characters (displayed characters) is represented by NCR (Numeric character reference, such as "Kanji", which is converted to "kanji"), so that the encoding can be displayed correctly in any coded environment.
Add the following three lines of code to the head of the PHP file:
Mb_internal_encoding ("gb2312");
Here the gb2312 is your site's original code
mb_http_output ("html-entities"); Ob_start (' Mb_output_handler ');
You use the Mb_convert_encoding function to enable the mbstring (multi-byte string) extension of PHP.
If you don't have mbstring extensions for PHP, you'll need to make the following settings so that PHP supports the extension.
1. Windows Server Environment
Edit php.ini file, will; Extension=php_mbstring.dll in front of; Remove and restart the Web server.
2, Linux Server environment
When compiling the configuration, add the--ENABLE-MBSTRING=CN compilation parameters, and then compile the PHP installation.
Conclusion:
If you are in the process of using PHP to encode and convert functions, you can see the PHP manual.