There are a large number of functions in PHP to support our requirements. The function described below is about the PHP encoding conversion function. We hope to help you. The Mb_convert_encoding function is an intra-PHP multi-byte string encoding conversion function.
Can be used in the necessary situations (such as: To solve the problem of using AJAX generated in the GB2312 encoding environment of the text garbled) to facilitate the encoding conversion to solve the problem of garbled Web pages, the use is very convenient, very efficient, almost all encoding support. PHP 4 >= 4.0.6, PHP 5 version support.
PHP Code conversion function prototype:
/**
* Multi-byte string encoding conversion function
*
* @param string str needs to encode the converted strings
* @param string to_encoding specified to be converted to some kind of encoding
, such as: gb2312, GBK, utf-8, etc.
* @param mixed From_encoding Mix to specify the encoding of the original string
, such as: simultaneous designation of JIS, Eucjp-win, Sjis-win mixed code
* @return String
String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding])
PHP Code Conversion Functions use an example:
1. Convert GBK encoded string into UTF-8 encoded 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 encoded string
Note that this file is saved to UTF-8 encoded format file for re-test <? 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 applies to all coding environments. In this way, the character set of the first 128 characters (display character) is used in NCR (Numeric character reference, such as "kanji" will be converted to "Chinese characters" this form), so that the code in any encoding environment, the page can display normally.
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 ');
Use the Mb_convert_encoding function to enable the PHP mbstring (multi-byte string) extension.
If there is no mbstring extension for PHP, you need to do the following to allow PHP to support 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 compile parameters, and then compile and install the PHP.
Conclusion:
If you are in the process of using PHP code conversion functions, there is nothing to understand, you can see the PHP manual.