>  기사  >  백엔드 개발  >  PHP는 ICU 확장 intl을 사용하여 중국어 문자를 병음으로 빠르게 변환하고 병음의 첫 글자를 기준으로 그룹화 및 정렬하여 정렬합니다.

PHP는 ICU 확장 intl을 사용하여 중국어 문자를 병음으로 빠르게 변환하고 병음의 첫 글자를 기준으로 그룹화 및 정렬하여 정렬합니다.

高洛峰
高洛峰원래의
2016-11-23 14:15:331878검색

ICU(International Components for Unicode)는 다른 언어(예: 중국어 간체)를 라틴어 표현으로 쉽게 변환할 수 있는 음역기
를 제공합니다.
http://cn2.php. zh/transliterator.transliterate.php
음역기: 다양한 언어로 문자열의 라틴어 표현을 얻을 수 있습니다.

<?php
//文件编码要求是Unicode
header(&#39;Content-Type: text/html; charset=utf-8&#39;);

echo transliterator_transliterate(&#39;Any-Latin&#39;, &#39;中华有为&#39;);
//输出 zhōng huá yǒu wèi

echo transliterator_transliterate(&#39;Any-Latin; Latin-ASCII; Lower()&#39;, &#39;中华有为&#39;);
//输出 zhong hua you wei

echo transliterator_transliterate(&#39;Any-Latin; Latin-ASCII; Upper()&#39;, &#39;中华有为&#39;);
//输出 ZHONG HUA YOU WEI

echo transliterator_transliterate(&#39;Any-Latin&#39;, &#39;重阳&#39;);
//输出 zhòng yáng (错误,多音字还是坑)

Apple의 CFStringTransform/kCFStringTransformToLatin도 ICU 변환을 통해 구현됩니다.
http:// userguide.icu-project.org/transforms/general#TOC-ICU-Transliterators
http://nshipster.com/cfstringtransform/

php5-intl(종속성 ICU:libicu52)의 중국어 간체 사용 (zh_CN) 분류기 콜레이터는 병음으로 정렬합니다:
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(&#39;Content-Type: text/html; charset=utf-8&#39;);
$coll = collator_create(&#39;zh_CN&#39;);
$arr = array(&#39;中国&#39;,&#39;华山&#39;,&#39;华夏&#39;,&#39;中华&#39;,&#39;重阳&#39;,&#39;重量&#39;,&#39;b&#39;,&#39;a&#39;,2,1);
collator_sort($coll, $arr);
var_export($arr);
//输出(可见汉字按照拼音排序,但不能识别多音字"重"):
array (
  0 => &#39;a&#39;,
  1 => &#39;b&#39;,
  2 => &#39;华山&#39;,
  3 => &#39;华夏&#39;,
  4 => &#39;中国&#39;,
  5 => &#39;中华&#39;,
  6 => &#39;重量&#39;,
  7 => &#39;重阳&#39;,
  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:
phpicu*.dll
phpextphp_intl.dll

다음은 중국어 병음의 첫 글자를 기준으로 그룹화 및 정렬하는 일반적으로 사용되는 기능을 구현합니다.

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

첨부:
ls 명령, Linux의 파일 관리자
1 2 a b Huashan Huaxia China Zhonghua Weight Chongyang
숫자, 문자, 한자(병음순으로 정렬되나 다중음성문자는 인식되지 않음)
한자의 경우, 다음 자연적인 정렬은 위와 다릅니다:

<?php
header(&#39;Content-Type: text/plain; charset=utf-8&#39;);
$arr = array(&#39;中国&#39;,&#39;华山&#39;,&#39;华夏&#39;,&#39;中华&#39;,&#39;重阳&#39;,&#39;重量&#39;,&#39;b&#39;,&#39;a&#39;,2,1);
natsort($arr); // 等价于 uasort($arr, function($a, $b) { return strnatcmp($a, $b); });
var_export($arr);
//输出(自然排序下汉字并没有按照拼音进行排序):
array (
  9 => 1,
  8 => 2,
  7 => &#39;a&#39;,
  6 => &#39;b&#39;,
  3 => &#39;中华&#39;,
  0 => &#39;中国&#39;,
  2 => &#39;华夏&#39;,
  1 => &#39;华山&#39;,
  5 => &#39;重量&#39;,
  4 => &#39;重阳&#39;,
)

여러 정렬의 비교:

<?php
header(&#39;Content-Type: text/plain; charset=utf-8&#39;);
$arr = explode(&#39; &#39;, &#39;1 11 111 112 12 121 122 a aa aaa aab ab aba abb 阿里 百度 中 中国 中国国 中国中 中中 中中国 中中中&#39;);
shuffle($arr); //打乱数组
//collator_sort(collator_create(&#39;zh_CN&#39;), $arr);
//usort($arr, function($a, $b) { return strnatcmp($a, $b); });
usort($arr, function($a, $b) { return strcmp($a, $b); });
echo implode(&#39; &#39;,$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 中 中中 中中中 中中国 中国 中国中 中国国 百度 阿里


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 객체 상속다음 기사:PHP 객체 상속