Home >Backend Development >PHP Tutorial >Chinese coding issues in PHP program development_PHP tutorial
The problem of Chinese encoding in PHP programming has troubled many people. The reason for this problem is actually very simple. Each country (or region) stipulates the character encoding set for computer information exchange, such as the extended ASCII code of the United States. China's GB2312-80, Japan's JIS, etc. As the basis for information processing in this country/region, character encoding sets play an important role in unifying encoding. Character encoding sets are divided into two categories according to length: SBCS (single-byte character set) and DBCS (double-byte character set). In early software (especially operating systems), in order to solve the computer processing of local character information, various localized versions (L10N) appeared. In order to differentiate, concepts such as LANG and Codepage were introduced. However, due to the overlapping code ranges of various local character sets, it is difficult to exchange information with each other; the cost of independent maintenance of each localized version of the software is high. Therefore, it is necessary to extract the commonalities in localization work and process them consistently to minimize special localization processing content. This is also called internationalization (118N). Various language information is further standardized as Locale information. The underlying character set processed became Unicode, which contains almost all glyphs.
Nowadays, most of the core character processing of software with international characteristics is based on Unicode. When the software is running, the corresponding local character encoding settings are determined according to the locale/Lang/Codepage settings at that time, and based on this Handle local characters. During the processing, it is necessary to convert between Unicode and local character sets, or even between two different local character sets with Unicode as an intermediate. This method is further extended in the network environment, and any character information at both ends of the network also needs to be converted into acceptable content according to the character set settings. Ip.
Character set encoding issues in databases
Popular relational database systems all support database character set encoding, which means that you can specify its own character set settings when creating a database. The data is stored in the specified encoding. When an application accesses data, there will be character set encoding conversion at entry and exit. For Chinese data, the database character encoding setting should ensure the integrity of the data. GB2312, GBK, UTF-8, etc. are all optional database character set encodings; of course we can also choose ISO8859-1 (8-bit), but we have to
before the application writes data. A 16-bit Chinese character or Unicode is split into two 8-bit characters. After reading the data, you need to merge the two bytes and identify the SBCS characters. Therefore, we do not recommend using ISO8859-1 as the database. Character set encoding. Not only does this not make full use of the character set encoding support of the database itself, but it also increases the complexity of programming. When programming, you can first use the management functions provided by the database management system to check whether the Chinese data is correct.
Before querying the database, the PHP program first executes mysql_query("SET NAMES xxxx"); where xxxx is the encoding of your web page (charset=xxxx). If charset=utf8 in the web page, then xxxx=utf8, if In the web page, charset=gb2312, then xxxx=gb2312. Almost all WEB programs have a common code to connect to the database, which is placed in a file. In this file, just add mysql_query("SET NAMES xxxx").
SET NAMES Displays what character set is used in the SQL statement sent by the client. Therefore, the SET NAMES 'utf-8' statement tells the server that "future messages from this client will use the character set utf-8." It also specifies the character set for the results that the server sends back to the client (for example, if you use a SELECT statement, it indicates what character set is used for the column values).
Commonly used techniques when locating problems:
Locating Chinese encoding problems usually uses the stupidest and most effective method - printing the internal code of the string after processing by the program you think is suspicious. By printing the internal code of a string, you can find out when Chinese characters are converted to Unicode, when Unicode is converted back to Chinese internal code, when one Chinese character becomes two Unicode characters, when a Chinese string is converted to A string of question marks, when were the high bits of the Chinese string cut off...
Using a suitable sample string can also help distinguish the type of question. For example: "aaah aa?@aa" and other Chinese and English character strings with both GB and GBK characteristic characters. Generally speaking, English characters will not be distorted no matter how they are converted or processed (if you encounter them, you can try to increase the length of consecutive English letters).