Php string encoding conversion example

Source: Internet
Author: User
Php string encoding conversion example

  1. Header ("content-type: text/html; charset = utf-8 ");
  2. Echo mb_convert_encoding ("You are my good friend", "UTF-8", "gbk ");
  3. ?>

2. convert UTF-8 encoded strings to gb2312 encoded strings.

  1. // Remember to save the file to a UTF-8 encoded file before testing
  2. Header ("content-type: text/html; charset = gb2312 ");
  3. Echo mb_convert_encoding ("You are my good friend", "gb2312", "UTF-8 ");
  4. ?>
  5. // Remember to save the file to a UTF-8 encoded file before testing
  6. Header ("content-type: text/html; charset = gb2312 ");
  7. Echo mb_convert_encoding ("You are my good friend", "gb312", "UTF-8 ");
  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:

  1. Mb_internal_encoding ("gb2312"); // The original code of the gb2312 website
  2. Mb_http_output ("html-entities ");
  3. 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.

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.