Detect System Default Encoding

This section provides a tutorial example on how to detect the system default encoding with a PHP script that displays the same Chinese text in 4 different encodings.

If you are using a new system to process text files in Chinese characters, the first thing you need to do is to figure out what is the default encoding on the interface. Understanding the system default encoding will help you to detect the encoding used a in Chinese text file, convert its encoding, and fix its encoding issue.

One way to figure this out is to use the following simple PHP script, Chinese-Encoding-Test.php.

\n");
  print("<body>\n");
  print("<h4>Chinese text in different encodings</h4>\n");
  print("<pre>\n");
  print("Unicode: ".$unicode."$\n");
  print("UTF-8: ".$utf8."$\n");
  print("GB18030: ".$gb18030."$\n");
  print("Big5: ".$big5."$\n");
  print("</pre>\n");
  print("</body>\n");
  print("</html>\n");
?>

Notes on this test script:

When you run this test PHP script on your system's terminal (or command window), you should see one of the encodings displaying correct Chinese text, if the console supports Chinese characters. Other encodings will display junk characters. That encoding is the default encoding of your system.

For example, the default encoding of my system is UTF-8. So I see the following output on my terminal:

Detecting System Default Encoding
Detecting System Default Encoding

If you see all encodings displaying junk characters, then your system does not support Chinese characters.

You can also run this test PHP script on a Web server to test Web browser encodings. Here is what I see on my Web browser with GBK (GB18030) encoding:

Testing Chinese Encoding in Web Browser
Testing Chinese Encoding in Web Browser

Table of Contents

 About This Book

 PHP Installation on Windows Systems

 Integrating PHP with Apache Web Server

 charset="*" - Encodings on Chinese Web Pages

 Chinese Characters in PHP String Literals

 Multibyte String Functions in UTF-8 Encoding

 Input Text Data from Web Forms

 Input Chinese Text Data from Web Forms

 MySQL - Installation on Windows

 MySQL - Connecting PHP to Database

 MySQL - Character Set and Encoding

 MySQL - Sending Non-ASCII Text to MySQL

 Retrieving Chinese Text from Database to Web Pages

 Input Chinese Text Data to MySQL Database

Chinese Text Encoding Conversion and Corruptions

Detect System Default Encoding

 Root Cause of Corrupted Chinese Text

 Corrupted Chinese File Name with Un-ZIP

 Generate 8-Bit Encoding Tables

 Restore Corrupted Chinese Text

 Encoding-Convertor.php - Encoding Conversion Test

 Archived Tutorials

 References

 Full Version in PDF/EPUB