PHP Chinese Character coding conversion method explaining

Source: Internet
Author: User
This paper introduces some knowledge of Chinese character coding conversion in PHP, analyzes the principle and method of PHP code conversion, and has the need of a friend reference.

For an understanding of the mysql4.1 character set, tell how PHP adapts to this change in MySQL. The same applies to MYSQL5 and above versions.

A There are two concepts in the MySQL character set, one is "character set (character set)" and the other is "collations". 1. Collationscollations translated into Chinese is "calibration", in the course of web development, the term, only in MySQL, the main role is to guide the MySQL comparison of characters, for example, in the ASCII character set, collations rules a less than B, A equals a, and a is equal to a. In general, you can basically ignore the existence of collations, because each character set has a default collations, which is usually the default collations to use. 2. Character sets in contrast to this, the character set is a broader concept, even under Windows ordinary text files, but also to the problem of character sets. Different character sets, which specify how different characters are encoded. A character set (character set) is a set of symbols and encodings, such as the ASCII character set, which includes characters such as numbers, uppercase and lowercase letters, semicolons, line breaks, and so on, encoded by a 7bit representation of a character (the encoding of A is the 65,B encoding is 98). ASCII only specifies the English alphabet encoding, non-English language can not be expressed in ASCII code, for this reason, different countries have to encode their own language, for example, our country, there is gb2312 code. But the coding between each country is different, there are some cross-platform problems, for this reason, some international standards organization, has developed some internationally common code, the most commonly used is UTF8. ASCII only to the English symbols and English alphabet coding, gb2312 on the English symbols, English letters, Chinese characters have been encoded, UTF8 all the language of the world to encode, so, gb1212 characters contain ASCII characters, UTF8 contains gb2312 characters. Thus, UTF8 is the character set with the widest character, so in some multilingual web systems, the UTF8 character set (phpmyadmin using UTF8 encoding) is generally used. The storage of any text is infiltrated into the concept of a character set. Includes a database, as well as regular text files.

Main terms: Characters: Kanji, English alphabet, punctuation, Latin, etc. Encoding: Converts a character to a computer-stored format, for example, a is represented by 65. Character set: A set of characters and the corresponding encoding method. A. MySQL character set MySQL currently supports multi-character sets, and supports conversion between different character sets (portability and support for multiple languages). MySQL can set the server-level character set, the database-level character set, the data table-level character set, the table column of the character set, in fact, the final use of the character set is the column where the character is stored, for example, you set Table1 col1 column is a character type, col1 used the character set, If the col2 column of the Table1 table is of type int, col2 does not use the concept of a character set. The server-level character set, the database-level character set, and the data-table-level character set are the default options for the column's character set. MySQL must have a character set that can be specified at start-up parameters, at compile time, or in a configuration file. MySQL server character set, just as the default value for database level. When you create a database, you can specify a character set, and if not specified, use the server's character set. Similarly, when you create a table, you can specify a table-level character set, and if not specified, use the database's character set as the table's character set. When you create a column, you can specify the character set of a column and, if not specified, the character set of the table. Typically, you only need to set the server-level character set, other database-level, table-level, and column-level character sets, all inherited from the server-level character set. Since UTF8 is the most extensive character set, in general, we set the MySQL server-level character set to utf8!

B. Character set problem for plain text any text stored, there is a problem with the character set, ordinary text file is no exception. windows2000+ system, open Notepad, "Save As ..." dialog box, there is an option that lets you choose how to store text encoding. Usually, everyone uses the windows2000+ system, all using the default encoding, so that the problem of character sets is not encountered. Under Windows, when you save a text file, you can choose how you encode it, but when you open a text file, it is automatically encoded. Online there is a windows2000+ Notepad to play mobile, unicom jokes, we can search, is because of windows in the Open text file, coding errors caused by error. Because auto-judgment coding can sometimes be wrong, there are text files that specify how to identify the encoding that you use. The HTML file is one such example. HTML is a text file. When storing an HTML file, you need to use an encoding, and in the HTML file, you also use HTML syntax, which specifies the encoding (for example) used by the file. If the HTML file does not specify an encoding, the browser automatically recognizes the encoding of the file. If the HTML specifies an encoding, the browser uses the encoding specified by the HTML. Usually, the HTML file specified by the CharSet and HTML file encoding is consistent, but there are inconsistencies, if inconsistent, will cause the Web page garbled (garbled here, only related to text files, and database-independent.) Using specialized Web page editing tools (such as Dreamwave), the files are automatically encoded according to the CharSet values in the page.

C. php+mysql character Set problem PHP eventually generates a text file, but he wants to take the text from the database, or save the text in the database. Because MySQL supports multiple character sets, by default, MySQL does not know what coded characters PHP sends to him, so MySQL asks the client (PHP) to tell him what character sets are being accessed. By setting character_set_client, PHP tells mysql,php what encoding is stored in the database. PHP sets Character_set_results to tell mysql,php what encoding data needs to be taken. PHP sets Character_set_connection, tells mysql,php the text in the query, and uses what encoding. MySQL stores text using the encoding of the settings. Assuming that MySQL uses Setserver to store text, PHP character_set_client is setclient,php character_set_results is setresult. Then, MySQL will send php text, from setclient encoding, converted to setserver encoding, and then into the database, if PHP to take text, MySQL to convert the text from Setserver to Setresult, and then sent to PHP. PHP file (the resulting HTML file) itself has a code, if the MySQL passed the code, and the PHP file itself, the code is different, then, the entire Web page, must be garbled. So, PHP generally will own the encoding method, tell MySQL. To ensure that the code is not garbled, you must unify the three: one is the encoding of the Web page itself, the second is the encoding specified in the HTML, and the third is the PHP code to tell MySQL (including character_set_client and Character_set_results). The first and second encodings, if using an editor such as DW, are usually consistent, but pages written in Notepad may not be consistent. The third encoding requires manual notification of MySQL. This can be done by using mysql_query ("Set names Characterx") in PHP.

D. Character set conversion problems if the small set is converted to a large character set, no data is lost, but the big print, converted to fine sets, may lose data. For example, there are characters in UTF8, gb2312 do not necessarily have, so converting from UTF8 to gb2312 may lose some characters. But there is an exception, first from gb2312 to UTF8, and then from UTF8 to gb2312, this situation is not lost data, because, just beginning to convert the text, are gb2312 characters, so, the whole process is gb2312 character in the conversion, will not be lost. Because UTF8 can accommodate all the characters in the world, the database is generally encoded using UTF8. This allows any character to be stored in a UTF8 encoded database.

E. phpMyAdmin garbled problem phpMyAdmin support multi-language, this must require the HTML page to use UTF8 encoding. HTML pages use UTF8 encoding, which must require phpMyAdmin to connect to MySQL, character_set_client and character_set_results use UTF8 encoding. In the current case, the PHP connection to MySQL can only use set names (or several other statements) to notify the MySQL encoding method, if there is no explicit declaration encoding method, will use Latin1 encoding. General procedures, are not explicitly declared character_set_client variables, so, are gb2312 text, Latin1 encoding method exists in the database, phpMyAdmin and then read in UTF8 format, is certainly garbled. If the PHP program is stored in the correct encoding database, there must be no problem. Therefore, it is not phpmyadmin that needs to be modified. (although sometimes modifying phpmyadmin can solve garbled problems, but this is not the root of the problem)

Two Concluding article

1. The database uses UTF8 storage (modify/etc/my.cnf, in [mysqld] segment plus DEFAULT-CHARACTER-SET=UTF8) (existing database, first to UTF8 format) 2.php program executes MySQL before querying the database _query ("Set names xxxx"), where xxxx is your Web page encoding (charset=xxxx), if the page Charset=utf8, then Xxxx=utf8, if the page charset=gb2312, then xxxx= gb2312, if the Web page of charset=ipaddr, then xxxx=ipaddr (joke, not this code) almost all Web programs, there is a connection to the database of common code, put in a file, in this file, add mysql_query ("set Names ") is OK. 3.phpmyadmin does not need to be changed. 4. Note that in order to ensure the actual encoding of the Web page (the encoding in the Windows Save dialog box) and the encoding of his declaration (charset=?) is consistent, please use tools such as DW to make Web pages.

  • 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.