There are three sorting methods for Chinese: 1. sort by pinyin 2. sort by strokes. 3. the default sorting method of the biased sorting system is pinyin sorting, which is also common. We will introduce sorting by pinyin. I hope you can help. There are three sorting methods for Chinese: 1. sort by pinyin 2. sort by strokes. 3. the default sorting method of the biased sorting system is pinyin sorting, which is also common. We will introduce sorting by pinyin. I hope you can help.
Script ec (2); script
1. Files in utf8 format need to be sorted in Chinese in the php array. Use gbk and gb2312. This is related to encoding in several formats. Gbk and gb2312 are encoded in pinyin.
The Code is as follows: |
|
Function utf8_array_asort (& $ array) { If (! Isset ($ array) |! Is_array ($ array )) { Return false; } Foreach ($ array as $ k => $ v) { $ Array [$ k] = iconv ('utf-8', 'gbk // IGNORE ', $ v ); } Asort ($ array ); Foreach ($ array as $ k => $ v) { $ Array [$ k] = iconv ('gbk', 'utf-8 // IGNORE ', $ v ); } Return true; } |
2. in MySQL, we often sort and query a field. However, when sorting and searching Chinese characters, the sorting and searching results of Chinese characters are often incorrect. This situation exists in many MySQL versions.
If this problem is not solved, MySQL cannot actually process Chinese characters. The reason for this problem is that MySQL is case insensitive when querying strings, in programming MySQL, The ISO-8859 character set is generally used as the default character set, therefore, the case-sensitivity conversion of Chinese encoding Characters During the comparison process causes this phenomenon.
Solution:
Add the "binary" attribute to a field that contains Chinese characters for binary comparison. For example, change "name char (10)" to "name char (10) binary ".
If you use the source code to compile MySQL, you can use the -- with -- charset = gbk parameter when compiling MySQL, so that MySQL can directly support Chinese search and sorting (latin1 by default ). You can also use extra-charsets = gb2312 and gbk to add multiple character sets.
If you do not want to modify the table structure or re-compile MySQL, you can also use the CONVERT function in the order by section of the query statement. For example
The Code is as follows: |
|
Select * from mytable order by CONVERT (chineseColumnName USING gbk ); |