PHP string Encoding Conversion example

Source: Internet
Author: User
    1. Header ("content-type:text/html; Charset=utf-8 ");
    2. Echo mb_convert_encoding ("You Are my good friend", "Utf-8", "GBK");
    3. ?>
Copy Code

2. Convert utf-8 encoded string into GB2312 encoded string

    1. Note Save this file as a UTF-8 encoded format file for re-testing
    2. Header ("content-type:text/html;charset=gb2312");
    3. Echo mb_convert_encoding ("You Are my good friend", "gb2312", "utf-8");
    4. ?>
    5. Note Save this file as a UTF-8 encoded format file for re-testing
    6. Header ("content-type:text/html; charset=gb2312 ");
    7. Echo mb_convert_encoding ("You Are my good friend", "gb312", "utf-8");
    8. ?>
Copy Code

3. Transform the entire page 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:

    1. Mb_internal_encoding ("Gb2312″"); GB2312 The original code of the website
    2. Mb_http_output ("Html-entities");
    3. Ob_start (' Mb_output_handler ');
Copy Code

Mb_internal_encoding ("Gb2312″"); GB2312 website Original Code 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.

  • 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.