PHP基于ICU扩展intl如何实现汉字转拼音及按拼音首字母分组排序

发布时间:2021-07-08 08:56:36 作者:小新
来源:亿速云 阅读:129

这篇文章给大家分享的是有关PHP基于ICU扩展intl如何实现汉字转拼音及按拼音首字母分组排序的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体如下:

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.

<?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汉字转拼音也是通过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

<?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排序后会出现在开头.

查看已经安装的软件包目录文件结构:

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

下面实现了常用的按汉字拼音首字母分组排序的功能:

<?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  华山  华夏  中国  中华  重量  重阳

数字,字母,汉字(按拼音排序,但不能识别多音字)

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

<?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); //打乱数组
//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 中 中中 中中中 中中国 中国 中国中 中国国 百度 阿里

感谢各位的阅读!关于“PHP基于ICU扩展intl如何实现汉字转拼音及按拼音首字母分组排序”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. PHP中的汉字怎么转拼音
  2. 得到汉字拼音首字母

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:Html5中如何使用localStorage存储JSON数据并读取JSON数据

下一篇:如何使用php实现CSV格式文件输出

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》