In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "PHP development appears Chinese garbled reasons and solutions," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's make up the following to take you to learn "PHP development appears Chinese garbled reasons and solutions" it!
I. The first is PHP web page coding.
The code of the php file itself should match the code of the web page.
a. If you want to use gb2312 encoding, then php to output header: header("Content-Type: text/html; charset=gb2312"), static page add, all files encoded in ANSI format, available Notepad open, save as select encoding ANSI, overwrite the source file.
b. If you want to use utf-8 encoding, php should output header("Content-Type: text/html; charset= utf-8"), static page addition, all files encoded in utf-8 format. Save as utf-8 may be a bit troublesome, generally utf-8 file will have BOM at the beginning, if you use session will have problems, you can use editplus to save, in editplus, Tools-> Parameter Selection-> File-> UTF-8 Signature, select Always Delete, and then save can remove BOM information.
2.php itself is not Unicode, all functions such as substr must be changed to mb_substr (mbstring extension required); or iconv transcoding.
Data interaction between PHP and Mysql
PHP and database coding should be consistent
1. Modify mysql configuration file my.ini or my.cnf, mysql*** encoded with utf8
[mysql] default-character-set=utf8 [mysqld] default-character-set=utf8 default-storage-engine=MyISAM Add under [mysqld]: default-collection =utf8_bin init_connect='SET NAMES utf8'
2. Add mysql_query("set names 'encoding'") before php program that needs to do database operation;, code is consistent with php code, if php code is gb2312 then mysql code is gb2312, if utf-8 then mysql code is utf8, so PHP Chinese garbled code will not appear when inserting or retrieving data
PHP is related to the operating system
Windows and Linux encoding is different, in Windows environment, when calling PHP function parameters if utf-8 encoding errors, such as move_uploaded_file(), filesize(), readfile(), etc., these functions are often used when processing upload, download, the following errors may appear 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 ..
In Linux environment, although these errors will not occur when gb2312 encoding is used, the file name after saving appears garbled code, which leads to the inability to read the file. In this case, the parameters can be converted to the code recognized by the operating system first. The code conversion can be mb_convert_encoding(character string, new code, original code) or iconv(original code, new code, character string). In this way, the file name saved after processing will not appear garbled code, and the file can be read normally, so as to realize the upload and download of Chinese name files.
In fact, there are better PHP Chinese garbled solution, completely separated from the system, also do not have to consider what the system code. You can generate a sequence of letters and numbers as the file name, and save the original name with Chinese in the database, so that there will be no problem calling move_uploaded_file(), just change the file name to the original name with Chinese when downloading. The code to download 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 file type,$file_name is the original name, and $file_path is the address of the file saved on the service.
At this point, I believe we have a deeper understanding of "PHP development appears Chinese garbled reasons and solutions," may wish to actually operate some bar! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.