Examples of UTF8 Chinese sorting in PHP and MYSQL, _ PHP Tutorial

Source: Internet
Author: User
Examples of UTF8 Chinese sorting in PHP and MYSQL ,. Example of UTF8 sorting in PHP and MYSQL: 1. you need to sort utf8 files in Chinese in the php array. However, files in UTF8 format cannot be sorted directly using asort. Use gbk and gb2312. This is an example of UTF8 sorting in PHP and MYSQL,

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 versions of MySQL (www.111cn.net.
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 );
From: http://www.111cn.net/phper/php-cy/67815.htm




Example of sorting by sort in Chinese: 1. you need to sort files in the php array in Chinese. However, files in the utf8 format cannot be sorted directly by asort. Use gbk and gb2312. This follows...

Related Article

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.