Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Chinese sorting method of UTF8 coding in PHP and MYSQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "the Chinese sorting method of UTF8 coding in PHP and MYSQL". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the Chinese sorting method of UTF8 coding in PHP and MYSQL.

This paper gives an example of the Chinese sorting method of UTF8 coding in PHP and MYSQL, which is shared with you for your reference. The specific implementation methods are as follows:

Generally speaking, there are three sorting methods in Chinese:

1. Sort according to Pinyin

two。 Sort by strokes

3. Sort by side.

The default sorting method of the system is Pinyin, which is also commonly used. The following is sorted by Pinyin.

1. You need to sort in Chinese in the php array, but you can't sort files in utf8 format directly with asort. You can use gbk and gb2312. This has something to do with coding in several formats. The codes of gbk and gb2312 themselves are sorted in pinyin.

The copy 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

}

two。 In MySQL, we often sort and query a field, but when sorting and searching in Chinese, the sorting and search results of Chinese characters are often wrong. This is true in many versions of MySQL.

If this problem is not solved, then MySQL will not be able to actually handle Chinese. The reason for this problem is that MySQL is case-insensitive when querying strings, and generally uses the ISO-8859 character set as the default character set when compiling MySQL, so this phenomenon is caused by the case conversion of Chinese coded characters in the process of comparison.

Solution:

Add a "binary" attribute to the fields containing Chinese to make them as binary comparisons, such as changing "name char (10)" to "name char (10) binary".

If you compile MySQL with source code, you can compile MySQL with the-- with--charset=gbk parameter, so that MySQL will directly support Chinese search and sorting (the default is latin1). You can also use extra-charsets=gb2312,gbk to add multiple character sets.

If you don't want to modify the table structure or recompile MySQL, you can also use the CONVERT function in the order by part of the query statement. such as

The copy code is as follows:

Select * from mytable order by CONVERT (chineseColumnName USING gbk)

At this point, I believe you have a deeper understanding of the Chinese sorting method of UTF8 coding in PHP and MYSQL, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report