Home  >  Article  >  Backend Development  >  PHP string encoding conversion example

PHP string encoding conversion example

WBOY
WBOYOriginal
2016-07-25 08:53:58786browse
  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 to gb2312 encoded string

  1. // Please save this file into a utf-8 encoding format 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. // Please save this file as a utf-8 encoding format 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. ?>
Copy code

3. Convert the entire page This method works in all coding environments. In this way, the character set other than the first 128 characters (display characters) is represented by ncr (numeric character reference, such as "Chinese characters" will be converted into "Chinese characters"). This encoding can be used in any encoding environment. normal display. Add the following three lines of code to the head of the php file:

  1. mb_internal_encoding("gb2312"); //The original encoding of gb2312 website
  2. mb_http_output("html-entities");
  3. ob_start('mb_output_handler');
Copy code

mb_internal_encoding(” gb2312″); // gb2312 The original encoding of the website mb_http_output("html-entities"); ob_start('mb_output_handler'); Using the mb_convert_encoding function requires enabling the mbstring (multi-byte string) extension of PHP.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn