Home > Article > Backend Development > Why does the PHP page appear HTML garbled characters?
Why does HTML garbled code appear on the PHP page?
1. PHP web page encoding error;
If you want to use gb2312 encoding, then PHP should output the header: header("Content-Type: text/html; charset=gb2312"), add it to the static page, and the encoding format of all files is ANSI. You can open it with Notepad and save it as the selected encoding. For ANSI, overwrite the source file.
If you want to use utf-8 encoding, then php should output the header: header("Content-Type: text/html; charset=utf-8"), add a static page, and 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 use editplus to save. In editplus, go to Tools->Parameter Selection->File-> UTF-8 signature, select Always delete, then save to remove the BOM information.
php itself is not Unicode, all substr and other functions must be changed to mb_substr (mbstring extension needs to be installed); or use iconv to transcode.
2. MySQL encoding is inconsistent with PHP encoding;
Solution:
Modify the mysql configuration file my.ini or my.cnf, mysql is the most Easy to use utf8 encoding [mysql] default-character-set=utf8 [mysqld] default-character-set=utf8 default-storage-engine=MyISAM Add under [mysqld]: default-collation=utf8_bin init_connect='SET NAMES utf8'
Add mysql_query("set names 'encoding'"); before the PHP program that needs to perform database operations. The encoding is consistent with the PHP encoding. If the PHP encoding is gb2312, then the mysql encoding is gb2312. If it is utf-8 Then the mysql encoding is utf8, so that there will be no garbled characters when inserting or retrieving data
3. The encoding of PHP and the operating system are inconsistent.
The encoding of Windows and Linux is different. In Windows environment, when calling PHP functions, if the parameters are utf-8 encoded, an error will occur, such as move_uploaded_file(), filesize(), readfile(), etc. These functions are often used when processing uploads and downloads. The following error may occur when calling:
Warning: move_uploaded_file()[function.move-uploaded-file]:failed to open stream: Invalid argument in ... Warning: move_uploaded_file()[function.move-uploaded-file]:Unable to move '' to '' in ... Warning: filesize() [function.filesize]: stat failed for ... in ... Warning: readfile() [function.readfile]: failed to open stream: Invalid argument in ..
Although using gb2312 encoding in a Linux environment These errors will not occur, but the saved file name will be garbled and the file cannot be read. In this case, you can first convert the parameters into the encoding recognized by the operating system. For encoding conversion, you can use 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, and the file can be read normally to achieve uploading and downloading of files with Chinese names.
In fact, there is a better solution, which is to completely separate from the system, so there is no need to consider the encoding of the system. You can generate a sequence of only letters and numbers as the file name, and save the original name with Chinese characters in the database. In this way, there will be no problem when calling move_uploaded_file(). When downloading, you only need to change the file name to the original name with Chinese characters. Chinese name. The code to implement downloading is as follows
header("Pragma: public"); header("Expires: 0"); header("Cache-Component: must-revalidate, post-check=0, pre-check=0"); header("Content-type: $file_type"); header("Content-Length: $file_size"); header("Content-Disposition: attachment; filename=\"$file_name\""); header("Content-Transfer-Encoding: binary"); readfile($file_path);
$file_type is the type of file, $file_name is the original name, and $file_path is the address of the file saved on the service.
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of Why does the PHP page appear HTML garbled characters?. For more information, please follow other related articles on the PHP Chinese website!