欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP基于ICU擴展intl快速實現(xiàn)漢字轉(zhuǎn)拼音及按拼音首字母分組排序的方法

 更新時間:2017年05月03日 11:43:58   作者:eechen  
這篇文章主要介紹了PHP基于ICU擴展intl快速實現(xiàn)漢字轉(zhuǎn)拼音及按拼音首字母分組排序的方法,結(jié)合實例形式分析了ICU擴展intl的實現(xiàn)方法與拼音轉(zhuǎn)換、排序等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了PHP基于ICU擴展intl快速實現(xiàn)漢字轉(zhuǎn)拼音及按拼音首字母分組排序的方法。分享給大家供大家參考,具體如下:

ICU(International Components for Unicode)里提供了transliterator(直譯器),
可以很方便把其他語言(比如簡體中文)轉(zhuǎn)為拉丁文表示:
http://cn2.php.net/manual/zh/transliterator.transliterate.php
Transliterator: allows getting latin representation of strings in various languages.

<?php
//文件編碼要求是Unicode
header('Content-Type: text/html; charset=utf-8');
echo transliterator_transliterate('Any-Latin', '中華有為');
//輸出 zhōng huá yǒu wèi
echo transliterator_transliterate('Any-Latin; Latin-ASCII; Lower()', '中華有為');
//輸出 zhong hua you wei
echo transliterator_transliterate('Any-Latin; Latin-ASCII; Upper()', '中華有為');
//輸出 ZHONG HUA YOU WEI
echo transliterator_transliterate('Any-Latin', '重陽');
//輸出 zhòng yáng (錯誤,多音字還是坑)

蘋果上的CFStringTransform/kCFStringTransformToLatin漢字轉(zhuǎn)拼音也是通過ICU transform實現(xiàn)的:

http://userguide.icu-project.org/transforms/general#TOC-ICU-Transliterators
http://nshipster.com/cfstringtransform/

使用php5-intl(依賴ICU:libicu52)的簡體中文(zh_CN)排序器collator按拼音排序:

http://cn2.php.net/manual/zh/collator.sort.php
php-src/ext/intl --enable-intl --with-icu-dir=DIR

相關(guān): MySQL數(shù)據(jù)表排序規(guī)則COLLATE=utf8_general_ci

<?php
header('Content-Type: text/html; charset=utf-8');
$coll = collator_create('zh_CN');
$arr = array('中國','華山','華夏','中華','重陽','重量','b','a',2,1);
collator_sort($coll, $arr);
var_export($arr);
/*輸出(可見漢字按照拼音排序,但不能識別多音字"重"):
array (
 0 => 'a',
 1 => 'b',
 2 => '華山',
 3 => '華夏',
 4 => '中國',
 5 => '中華',
 6 => '重量',
 7 => '重陽',
 8 => 1,
 9 => 2,
)
*/

如果元素1和2加上引號變成字符串類型的話,則1和2排序后會出現(xiàn)在開頭.

查看已經(jīng)安裝的軟件包目錄文件結(jié)構(gòu):

dpkg -L libicu52:amd64
/usr/lib/x86_64-linux-gnu/libicu*
/usr/lib/x86_64-linux-gnu/libicudata.so.52.1 動態(tài)庫23MB
/usr/lib/x86_64-linux-gnu/libicudata.a       靜態(tài)庫23MB

Windows上則是:

php\icu*.dll
php\ext\php_intl.dll

下面實現(xiàn)了常用的按漢字拼音首字母分組排序的功能:

<?php
header('Content-Type: text/html; charset=utf-8');
$arr = array('百度知道','阿里云','百度百科','阿里巴巴');
$coll = collator_create('zh_CN');
collator_sort($coll, $arr);
var_export($arr);
//輸出 array ( 0 => '阿里巴巴', 1 => '阿里云', 2 => '百度百科', 3 => '百度知道', )
$tmp = array();
foreach($arr as $v) {
 $pinyin = transliterator_transliterate('Any-Latin; Latin-ASCII; Upper()', $v);
 $tmp[substr($pinyin, 0, 1)][] = $v;
}
var_export($tmp);
/*輸出
array (
 'A' =>
 array (
  0 => '阿里巴巴',
  1 => '阿里云',
 ),
 'B' =>
 array (
  0 => '百度百科',
  1 => '百度知道',
 ),
)
*/

附:

ls命令,Linux和Windows的文件管理器,顯示如下:

1  2  a  b  華山  華夏  中國  中華  重量  重陽

數(shù)字,字母,漢字(按拼音排序,但不能識別多音字)

漢字方面,下面的自然排序跟上面有所不同:

<?php
header('Content-Type: text/plain; charset=utf-8');
$arr = array('中國','華山','華夏','中華','重陽','重量','b','a',2,1);
natsort($arr); // 等價于 uasort($arr, function($a, $b) { return strnatcmp($a, $b); });
var_export($arr);
/*輸出(自然排序下漢字并沒有按照拼音進行排序):
array (
 9 => 1,
 8 => 2,
 7 => 'a',
 6 => 'b',
 3 => '中華',
 0 => '中國',
 2 => '華夏',
 1 => '華山',
 5 => '重量',
 4 => '重陽',
)
*/

幾種排序的比較:

<?php
header('Content-Type: text/plain; charset=utf-8');
$arr = explode(' ', '1 11 111 112 12 121 122 a aa aaa aab ab aba abb 阿里 百度 中 中國 中國國 中國中 中中 中中國 中中中');
shuffle($arr); //打亂數(shù)組
//collator_sort(collator_create('zh_CN'), $arr);
//usort($arr, function($a, $b) { return strnatcmp($a, $b); });
usort($arr, function($a, $b) { return strcmp($a, $b); });
echo implode(' ',$arr);
exit();
?>

ls排序:

1 11 111 112 12 121 122 a aa aaa aab ab aba abb 阿里 百度 中 中國 中國國 中國中 中中 中中國 中中中

collator_sort(zh_CN)排序(類似Windows/Linux桌面文件管理器里的默認按名稱上升排列):

1 11 12 111 112 121 122 a aa aaa aab ab aba abb 阿里 百度 中 中國 中國國 中國中 中中 中中國 中中中

strnatcmp排序:

1 11 12 111 112 121 122 a aa aaa aab ab aba abb 中 中中 中中中 中中國 中國 中國中 中國國 百度 阿里

strcmp排序:

1 11 111 112 12 121 122 a aa aaa aab ab aba abb 中 中中 中中中 中中國 中國 中國中 中國國 百度 阿里

PS:這里再為大家推薦2款比較實用的相關(guān)在線排序工具供大家參考使用:

在線中英文根據(jù)首字母排序工具:
http://tools.jb51.net/aideddesign/zh_paixu

在線文本倒序翻轉(zhuǎn)排序工具:
http://tools.jb51.net/aideddesign/flipped_txt

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php排序算法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php常用函數(shù)與技巧總結(jié)》、《PHP錯誤與異常處理方法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論