最新消息:文章中包含代码时,请遵守代码高亮规范!

php对中文按照首字母排序【原创】

杂项 Alex 116浏览 0评论

/**
* 中文字符拼音首字母
*
* @param $str
* @return null|string
*/
public function getFirstLetter($str)
{
if (empty($str)) {
return ”;
}

$fir = $fchar = ord($str[0]);
if ($fchar >= ord(‘A’) && $fchar <= ord(‘z’)) {
return strtoupper($str[0]);
}

$s1 = @iconv(‘UTF-8’, ‘gb2312’, $str);
$s2 = @iconv(‘gb2312’, ‘UTF-8’, $s1);
$s = $s2 == $str ? $s1 : $str;
if (!isset($s[0]) || !isset($s[1])) {
return ”;
}

$asc = ord($s[0]) * 256 + ord($s[1]) – 65536;

if (is_numeric($str)) {
return $str;
}

if (($asc >= -20319 && $asc <= -20284) || $fir == ‘A’) {
return ‘A’;
}
if (($asc >= -20283 && $asc <= -19776) || $fir == ‘B’ || in_array($str, array(‘璧’, ‘灞’))) {
return ‘B’;
}
if (($asc >= -19775 && $asc <= -19219) || $fir == ‘C’ || in_array($str, array(‘枞’, ‘茌’, ‘瀍’, ‘禅’, ‘岑’))) {
return ‘C’;
}
if (($asc >= -19218 && $asc <= -18711) || $fir == ‘D’ || in_array($str, array(‘砀’, ‘岱’, ‘儋’))) {
return ‘D’;
}
if (($asc >= -18710 && $asc <= -18527) || $fir == ‘E’) {
return ‘E’;
}
if (($asc >= -18526 && $asc <= -18240) || $fir == ‘F’ || in_array($str, array(‘芙’))) {
return ‘F’;
}
if (($asc >= -18239 && $asc <= -17923) || $fir == ‘G’ || in_array($str, array(‘珙’))) {
return ‘G’;
}
if (($asc >= -17922 && $asc <= -17418) || $fir == ‘H’ || in_array($str, array(‘亳’, ‘潢’, ‘濠’))) {
return ‘H’;
}
if (($asc >= -17417 && $asc <= -16475) || $fir == ‘J’ || in_array($str, array(‘泾’, ‘旌’, ‘莒’, ‘鄄’, ‘郏’, ‘犍’, ‘筠’))) {
return ‘J’;
}
if (($asc >= -16474 && $asc <= -16213) || $fir == ‘K’) {
return ‘K’;
}
if (($asc >= -16212 && $asc <= -15641) || $fir == ‘L’ || in_array($str, array(‘崂’, ‘岚’, ‘栾’, ‘漯’, ‘浏’, ‘醴’, ‘耒’, ‘澧’, ‘泸’, ‘寮’, ‘阆’, ‘麟’))) {
return ‘L’;
}
if (($asc >= -15640 && $asc <= -15166) || $fir == ‘M’ || in_array($str, array(‘渑’, ‘沔’, ‘汨’, ‘沐’, ‘湄’, ‘勐’))) {
return ‘M’;
}
if (($asc >= -15165 && $asc <= -14923) || $fir == ‘N’) {
return ‘N’;
}
if (($asc >= -14922 && $asc <= -14915) || $fir == ‘O’ || in_array($str, array(‘讴’))) {
return ‘O’;
}
if (($asc >= -14914 && $asc <= -14631) || $fir == ‘P’ || in_array($str, array(‘鄱’, ‘濮’, ‘郫’))) {
return ‘P’;
}
if (($asc >= -14630 && $asc <= -14150) || $fir == ‘Q’ || in_array($str, array(‘谯’, ‘杞’, ‘淇’, ‘硚’, ‘蕲’, ‘綦’, ‘邛’, ‘麒’, ‘岐’))) {
return ‘Q’;
}
if (($asc >= -14149 && $asc <= -14091) || $fir == ‘R’ || in_array($str, array(‘榕’))) {
return ‘R’;
}
if (($asc >= -14090 && $asc <= -13319) || $fir == ‘S’ || in_array($str, array(‘濉’, ‘泗’, ‘莘’, ‘嵩’, ‘睢’, ‘浉’))) {
return ‘S’;
}
if (($asc >= -13318 && $asc <= -12839) || $fir == ‘T’ || in_array($str, array(‘滕’, ‘郯’, ‘覃’, ‘潼’))) {
return ‘T’;
}
if (($asc >= -12838 && $asc <= -12557) || $fir == ‘W’ || in_array($str, array(‘婺’, ‘汶’, ‘莞’))) {
return ‘W’;
}
if (($asc >= -12556 && $asc <= -11848) || $fir == ‘X’ || in_array($str, array(‘歙’, ‘芗’, ‘浔’, ‘淅’, ‘猇’, ‘浠’, ‘溆’))) {
return ‘X’;
}
if (($asc >= -11847 && $asc <= -11056) || $fir == ‘Y’ || in_array($str, array(‘黟’, ‘颍’, ‘埇’, ‘弋’, ‘峄’, ‘兖’, ‘郓’, ‘荥’, ‘偃’, ‘鄢’, ‘郾’, ‘驿’, ‘攸’, ‘沅’, ‘邕’, ‘莺’))) {
return ‘Y’;
}
if (($asc >= -11055 && $asc <= -10247) || $fir == ‘Z’ || in_array($str, array(‘诏’, ‘柘’, ‘轵’, ‘秭’, ‘芷’, ‘浈’, ‘梓’))) {
return ‘Z’;
}

return ”;

}

//首字母排序(省略获取数据)
foreach ($data[‘info’] as $key => $val){
$return = $this->get(‘services.strings’)->getFirstLetter($val[‘user_name’]);

$data[‘info’][$key][‘key’] = $return;
}
$sort = array_column( $data[‘info’], ‘key’);
array_multisort($sort, SORT_ASC, $data[‘info’]);

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/24504.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Alex的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!