Home > Article > Backend Development > Chinese characters in php web page are garbled
Configuring phpstudy, Chinese garbled characters appear when accessing the page. The following is the solution.
1. Encoding of PHP web page
1. The encoding of the PHP file itself and the encoding of the web page should match
a . If you want to use gb2312 encoding, then php should output the header: header("Content-Type: text/html; charset=gb2312")
, and add <meta http-equiv=" to the static page Content-Type" content="text/html; charset=gb2312">
, the encoding format of all files is ANSI
, which can be opened with Notepad, save as and select the encoding as ANSI, overwriting the source file .
b. If you want to use utf-8 encoding, then php should output the header: header("Content-Type: text/html; charset=utf-8")
, add it to the static page <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
, the encoding format of all files is utf-8.
Saving as utf-8 may be a bit troublesome. Generally, utf-8 files will have BOM at the beginning. If you use session, there will be problems. You can save it with editplus. In editplus, go to Tools->Parameter Selection- >File->UTF-8 signature, select Always delete, then save to remove the BOM information.
2. PHP itself is not Unicode
All functions such as substr must be changed to mb_substr (mbstring extension needs to be installed), or iconv transcoding must be used.
2. Data interaction between PHP and Mysql
1. The coding of PHP and database should be consistent
Modify mysql configuration The file my.ini or my.cnf, mysql is best encoded with utf8.
#2. Add mysql_query("set names 'encoding'")
# before the PHP program that needs to perform database operations.
3. PHP is related to the operating system
The encoding of Windows and Linux is different. In the Windows environment, when calling a PHP function, if the parameter is utf- 8 Encoding errors will occur, such asmove_uploaded_file(),
filesize(),
readfile(), etc. These functions are often used when processing uploads and downloads. , an error may occur when calling.
mb_convert_encoding(string, new encoding, original encoding) or
iconv(original encoding, new encoding, string), so that the file name saved after processing will not be garbled. You can also read files normally and upload and download files with Chinese names.
move_uploaded_file(). When downloading, you only need to move the file The name was changed to the original Chinese name. The code to implement the download is as follows:
The above is the detailed content of Chinese characters in php web page are garbled. For more information, please follow other related articles on the PHP Chinese website!