Building Chinese Web Sites Using PHP - Version 2.20, by Dr. Herong Yang
Chinese Character String with GB18030 Encoding Error
This section provides a tutorial example showing a Chinese Web page generated by PHP using GB18030 encoding. But the page displays Chinese characters as garbled text.
Chinese character strings should use UTF-8 encoding. But for some reason, if you have to use GB18030 encoding for your Chinese character strings, you can use PHP string as binary strings to store Chinese character strings in GB18030 encoding. In order to output Chinese characters to Web pages and display them correctly, you need to:
Here is a simple test I did on my local system:
1. Run my Chinese text editor that supports GB18030 encoding.
2. Enter the following PHP script file:
<?php #- String-GB18030.php #- Copyright (c) 2005 HerongYang.com, All Rights Reserved. # $help = '?????????????'; print('<html>'); print('<meta http-equiv="Content-Type"'. ' content="text/html; charset=gb18030"/>'); print('<body>'); print('<b>Chinese string in GB18030 in PHP</b><br/>'); print($help.'<br/>'); print('</body>'); print('</html>'); ?>
You see some question marks (?) in the source code listed above, because this book uses UTF-8 encoding. GB18030 encoded characters can not be included here.
3. Save the as String-GB18030.php in GB18030 encoding. On my Chinese text editor, I had to select "GB text file" as the "Save as type" to ensure my document was saved in GB18030 encoding. Like many other Chinese text editors, it supports multiple encodings. If you are not careful, the document could be saved with a wrong encoding.
4. Copy String-GB18030.php to \local\apache\htdocs.
5. Now run Internet Explorer (IE) with http://localhost/String-GB18030.php. But surprisingly, Chinese characters are displayed incorrectly as garbled text:
See the next tutorial on how to troubleshoot and fix the issue.
Last update: 2019.
Table of Contents