Home > Backend Development > PHP Tutorial > PHP encoding conversion small example

PHP encoding conversion small example

WBOY
Release: 2016-07-25 08:53:56
Original
917 people have browsed it
  1. /*
  2. * Common encoding issues
  3. *
  4. * 1. Garbled code problem on the page:
  5. * The main reason for the garbled code problem on the page is that the encoding of the display font is inconsistent with the encoding displayed in the browser. , the
  6. * encoding output by the browser is controlled by
  7. * in the page file, and if you If the characters output in the program are garbled, then the font output by the program is not the same encoding as the one set above, then garbled characters will appear
  8. * 2. Garbled characters in the database
  9. * There is a default character encoding in the database. If you If the submitted character encoding is different from the encoding in the data, then the
  10. * displayed in the database will also be garbled characters
  11. * (Script Academy bbs.it-home.org)
  12. * Summary: The reason for the garbled characters is that there are two types of characters caused by different encodings, then we must find a way to make them the same
  13. *
  14. * Main points:
  15. * 1. header settings
  16. * 2. Database default encoding settings
  17. * 3. Editor encoding settings (sometimes this is very important,)
  18. * (The best way is to set these three encodings to be the same)
  19. *
  20. * Sometimes if garbled characters appear again, you can also use some encoding conversion functions to convert, as long as the conversion Just change it to the same encoding set in ,
  21. * Main encoding conversion function
  22. * mb_convert_encoding(str,to,from)
  23. * mb_convert_encoding("Hello everyone","gbk","utf-8") (The first encoding is the destination encoding to be converted, and the second is the encoding for the code editor you write)
  24. *
  25. * iconv(in,out,str)
  26. * */
  27. echo mb_convert_encoding("Hello everyone", "gbk","utf-8");//There will be garbled characters in utf-8, because what you output is the encoding converted to gbk
  28. echo iconv("utf-8","gbk","Everyone OK");
  29. ?>
Copy code


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template