ICU(International Components for Unicode)里提供了transliterator(直译器),
可以很方便把其他语言(比如简体中文)转为拉丁文表示:
http://cn2.php.net/manual/zh/transliterator.transliterate.php
Transliterator: allows getting latin representation of strings in various languages.
苹果上的
CFStringTransform/kCFStringTransformToLatin汉字转拼音也是通过ICU transform实现的:
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
相关: MySQL数据表排序规则COLLATE=utf8_general_ci
'a',
1 => 'b',
2 => '华山',
3 => '华夏',
4 => '中国',
5 => '中华',
6 => 1,
7 => 2,
)
查看已经安装的软件包目录文件结构:
dpkg -L libicu52:amd64
/usr/lib/x86_64-linux-gnu/libicu*
/usr/lib/x86_64-linux-gnu/libicudata.so.52.1 动态库23MB
/usr/lib/x86_64-linux-gnu/libicudata.a 静态库23MB
Windows上则是:
php\icu*.dll
php\ext\php_intl.dll
下面实现了常用的按汉字拼音首字母分组排序的功能:
'阿里巴巴', 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 => '百度知道',
),
)