php获取中文字符拼音首字母ASCII
场景:对中文字符串进行排序<?php
class demo { public function __construct() { } /** * @todo 获取中文字符串的字符首字母ascii * @param $string * @param int $default_length * @return float|int */ public function chinese_alphabet_ascii($string, $default_length = 3) { $string_length = mb_strlen($string); $number = 0; for ($i = 0; $i < $string_length; $i++) {
$number = $number * 100 + $this->chinese_initials(mb_substr($string, $i, $i + 1));
if($i === $default_length - 1) breack; } for ($j = $string_length; $j < $default_length; $j++) { $number *= 100; } return $number; } /** * @todo 获取汉字拼音首字母 - ASCII * @param $character * @return int */ public function chinese_initials($character) { $ascii = ord($character); if ($ascii > 127) { $unicode = iconv("UTF-8", "GB2312", $character); $ascii = ord($unicode{0}) * 256 + ord($unicode{1}) - 65536; } switch ($ascii) { // 0 ~ 9 case $ascii >= 48 && $ascii <= 57 : $result_ascii = $ascii; break; // A ~ Z case $ascii >= 65 && $ascii <= 90 : $result_ascii = $ascii; break; // a ~ z case $ascii >= 97 && $ascii <= 122 : $result_ascii = $ascii - 32; break; // 汉字字母 case $ascii >= -20319 && $ascii <= -20284: $result_ascii = 65; break; case $ascii >= -20283 && $ascii <= -19776: $result_ascii = 66; break; case $ascii >= -19775 && $ascii <= -19219: $result_ascii = 67; break; case $ascii >= -19218 && $ascii <= -18711: $result_ascii = 68; break; case $ascii >= -18710 && $ascii <= -18527: $result_ascii = 69; break; case $ascii >= -18526 && $ascii <= -18240: $result_ascii = 70; break; case $ascii >= -18239 && $ascii <= -17923: $result_ascii = 71; break; case $ascii >= -17922 && $ascii <= -17418: $result_ascii = 72; break; case $ascii >= -17417 && $ascii <= -16475: $result_ascii = 74; break; case $ascii >= -16474 && $ascii <= -16213: $result_ascii = 75; break; case $ascii >= -16212 && $ascii <= -15641: $result_ascii = 76; break; case $ascii >= -15640 && $ascii <= -15166: $result_ascii = 77; break; case $ascii >= -15165 && $ascii <= -14923: $result_ascii = 78; break; case $ascii >= -14922 && $ascii <= -14915: $result_ascii = 79; break; case $ascii >= -14914 && $ascii <= -14631: $result_ascii = 80; break; case $ascii >= -14630 && $ascii <= -14150: $result_ascii = 81; break; case $ascii >= -14149 && $ascii <= -14091: $result_ascii = 82; break; case $ascii >= -14090 && $ascii <= -13319: $result_ascii = 83; break; case $ascii >= -13318 && $ascii <= -12839: $result_ascii = 84; break; case $ascii >= -12838 && $ascii <= -12557: $result_ascii = 87; break; case $ascii >= -12556 && $ascii <= -11848: $result_ascii = 88; break; case $ascii >= -11847 && $ascii <= -11056: $result_ascii = 89; break; case $ascii >= -11055 && $ascii <= -10247: $result_ascii = 90; break; default: $result_ascii = 91; } return $result_ascii; } }
补充:&& 、|| 与 and、or 的优先级:
优先级:&& > || > and > or
1. 单独的两个表达参与运算,&&与and、||与or运算结果一样;
2.多个表达式参与运算时,注意优先级问题,例如:$a || $b and $c || $d 相当于 ($a || $b) and ($c || $d) 。