Chinese encoding in PHP Program Development (1)

Source: Internet
Author: User

In PHP programming, Chinese Encoding Problems Often plague many people who have no experience in solving such problems. In fact, the cause of this problem is very simple. Every country (or region) the delimiter set for computer information exchange, such as the extended ASCII code of the United States, GB2312-80 of China, JIS of Japan, etc. As the basis for information processing in the country/region, character encoding sets play an important role in unified encoding. The character Collation is divided into SBCS (single-byte character set) and DBCS (dubyte Character Set) by length. Early software (especially the operating system), in order to solve the computer processing of local character information, various local versions (L10N) were introduced. to distinguish, LANG, Codepage and other concepts were introduced. However, the Code ranges of local character sets overlap, making it difficult to exchange information between them. The independent maintenance costs of each localized version of the software are high. Therefore, it is necessary to extract the commonalities in the localization work for consistent processing, so as to minimize the content of special localization processing. This is the so-called International (118N ). The language information is further standardized as Locale information. The underlying character set to be processed becomes Unicode that contains almost all glyphs.

Currently, most of the software's core Character Processing Systems with internationalization features are Unicode-based. During software running, the corresponding local character encoding settings are determined based on the current ocale/Lang/Codepage settings, and handle local characters accordingly. In the process, Unicode and local character sets must be converted to each other, or two different local character sets with Unicode as the center must be converted to each other. This method is further extended in the network environment. The character information at both ends of any network needs to be converted to acceptable content according to the character set settings.

Character Set encoding in the database

Popular Relational Database Systems Support database character set encoding. That is to say, when creating a database, you can specify its own character set settings. database data is stored in the specified encoding format. When an application accesses data, character set encoding is converted at the entry and exit. For Chinese data, the character encoding settings of the database should ensure data integrity. GB2312, GBK, UTF-8 and so on are optional database character set encoding; of course we can also choose ISO8859-1 (8-bit), but we have

Before writing data to a program, split a 16-bit Chinese character or Unicode character into two 8-bit characters. After reading the data, you also need to combine the two bytes, we also need to identify the SBCS characters, so we do not recommend using ISO8859-1 as the database character set encoding. This not only does not make full use of the database's own character set encoding support, but also increases programming complexity. During programming, you can check whether the Chinese data is correct with the management function provided by the database management system.

Before the PHP program queries the database, it first executes mysql_query ("set names xxxx"). xxxx indicates the code of Your webpage (charset = xxxx). If charset = utf8 In the webpage, then xxxx = utf8. If charset = gb2312 In the webpage, then xxxx = gb2312. Almost all WEB programs have a public code to connect to the database and put it in a file, in this file, add mysql_query ("set names xxxx.

Set names displays the character sets used in the SQL statements sent by the client. Therefore, the set names 'utf-8' statement tells the server that "the information sent from this client will use the UTF-8 character SET ". It also specifies the character set for the results sent from the server back to the client (for example, if you use a SELECT statement, it indicates the character set used by the column value ).

Frequently Used troubleshooting skills

The most stupid and effective way to locate Chinese encoding problems is to print the string's internal code after you think the program is suspected of processing it. By printing the character string's internal code, you can find out when Chinese characters are converted to Unicode, when Unicode is converted back to Chinese characters, and when a Chinese character is converted to two Unicode characters, when is the Chinese string converted into a question mark? When is the high position of the Chinese string truncated ......

Selecting the appropriate sample string also helps to identify the type of the problem. For example, "aa, aa? @ Aa "is a string of all Chinese and English characters including GB and GBK. In general, English characters are not distorted no matter how they are converted or processed (if you encounter it, you can try to increase the length of consecutive English letters ).1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.