源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法

  • 时间:2020-09-11 14:34 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法
本文实例讲述了PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法。分享给大家供大家参考,具体如下: [b]ICU[/b](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 => '百度知道',
 ),
)
*/

[b]附:[/b] 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 => '重阳',
)
*/

[b]几种排序的比较:[/b]
<?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();
?>

[b]ls排序:[/b] 1 11 111 112 12 121 122 a aa aaa aab ab aba abb 阿里 百度 中 中国 中国国 中国中 中中 中中国 中中中 [b]collator_sort(zh_CN)排序[/b](类似Windows/Linux桌面文件管理器里的默认按名称上升排列): 1 11 12 111 112 121 122 a aa aaa aab ab aba abb 阿里 百度 中 中国 中国国 中国中 中中 中中国 中中中 [b]strnatcmp排序:[/b] 1 11 12 111 112 121 122 a aa aaa aab ab aba abb 中 中中 中中中 中中国 中国 中国中 中国国 百度 阿里 [b]strcmp排序:[/b] 1 11 111 112 12 121 122 a aa aaa aab ab aba abb 中 中中 中中中 中中国 中国 中国中 中国国 百度 阿里 [b]PS:这里再为大家推荐2款比较实用的相关在线排序工具供大家参考使用:[/b] [b]在线中英文根据首字母排序工具: [/b][url=http://tools.jb51.net/aideddesign/zh_paixu]http://tools.jb51.net/aideddesign/zh_paixu[/url] [b]在线文本倒序翻转排序工具: [/b][url=http://tools.jb51.net/aideddesign/flipped_txt]http://tools.jb51.net/aideddesign/flipped_txt[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/150.htm]php排序算法总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/168.htm]php常用函数与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/196.htm]PHP错误与异常处理方法总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部