Php string encoding conversion example
- 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 strings to gb2312 encoded strings.
- // 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", "gb2312", "UTF-8 ");
- ?>
- // 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. 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 pages in any encoding environment. Add the following three lines of code to the header of the PHP file:
- Mb_internal_encoding ("gb2312"); // The original code of the gb2312 website
- Mb_http_output ("html-entities ");
- Ob_start ('MB _ output_handler ');
Mb_internal_encoding ("gb2312"); // The original gb2312 website 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. |