当前位置:网站首页>Get the uppercase initials of Chinese Pinyin in PHP
Get the uppercase initials of Chinese Pinyin in PHP
2022-07-02 07:20:00 【Snow wind in the night sky】
Preface
In the project of project development, I encountered the function of returning a string of Chinese strings to uppercase initials , After learning from the methods of other developers on the Internet, I sorted out the best method .
Problem description
Need to put 【 Downstream rockfill area 】 convert to 【XYDSQ】.
Method example
if (!function_exists('getfirstchar')) {
/** * Get the capital first letter of Chinese Pinyin * @param [type] $str * @return void */
function getfirstchar($str)
{
$fchar = ord(substr((string) $str, 0, 1));
if (($fchar >= ord("a") and $fchar <= ord("z")) or ($fchar >= ord("A") and $fchar <= ord("Z"))) {
return strtoupper(chr($fchar));
}
$s = iconv("UTF-8", "GBK", (string) $str);
$asc = ord($s{
0}) * 256 + ord($s{
1}) - 65536;
if ($asc >= -20319 and $asc <= -20284) {
return "A";
}
if ($asc >= -20283 and $asc <= -19776) {
return "B";
}
if ($asc >= -19775 and $asc <= -19219) {
return "C";
}
if ($asc >= -19218 and $asc <= -18711) {
return "D";
}
if ($asc >= -18710 and $asc <= -18527) {
return "E";
}
if ($asc >= -18526 and $asc <= -18240) {
return "F";
}
if ($asc >= -18239 and $asc <= -17923) {
return "G";
}
if ($asc >= -17922 and $asc <= -17418) {
return "H";
}
if ($asc >= -17417 and $asc <= -16475) {
return "J";
}
if ($asc >= -16474 and $asc <= -16213) {
return "K";
}
if ($asc >= -16212 and $asc <= -15641) {
return "L";
}
if ($asc >= -15640 and $asc <= -15166) {
return "M";
}
if ($asc >= -15165 and $asc <= -14923) {
return "N";
}
if ($asc >= -14922 and $asc <= -14915) {
return "O";
}
if ($asc >= -14914 and $asc <= -14631) {
return "P";
}
if ($asc >= -14630 and $asc <= -14150) {
return "Q";
}
if ($asc >= -14149 and $asc <= -14091) {
return "R";
}
if ($asc >= -14090 and $asc <= -13319) {
return "S";
}
if ($asc >= -13318 and $asc <= -12839) {
return "T";
}
if ($asc >= -12838 and $asc <= -12557) {
return "W";
}
if ($asc >= -12556 and $asc <= -11848) {
return "X";
}
if ($asc >= -11847 and $asc <= -11056) {
return "Y";
}
if ($asc >= -11055 and $asc <= -10247) {
return "Z";
}
return null;
}
}
if (!function_exists('get_letter')) {
/** * Get the capital first letter of Chinese Pinyin * @param [type] $string * @return void */
function get_letter($string)
{
$charlist = preg_split('/(?<!^)(?!$)/u', (string) $string);
return implode(array_map("getfirstchar", $charlist));
}
}
Usage method
//【 Downstream rockfill area 】 convert to 【XYDSQ】
$zone_name_pinyin = get_letter(' Downstream rockfill area ');
边栏推荐
猜你喜欢

类加载器及双亲委派机制

oracle apex ajax process + dy 校验

User login function: simple but difficult

MySQL中的正则表达式

Basic knowledge of software testing

Yaml file of ingress controller 0.47.0

中年人的认知科普

【信息检索导论】第三章 容错式检索

ORACLE EBS 和 APEX 集成登录及原理分析

Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
随机推荐
Oracle EBS DataGuard setup
php中在二维数组中根据值返回对应的键值
Laravel8中的find_in_set、upsert的使用方法
ssm垃圾分类管理系统
Oracle apex 21.2 installation and one click deployment
mapreduce概念和案例(尚硅谷学习笔记)
ORACLE EBS DATAGUARD 搭建
The first quickapp demo
【Torch】最简洁logging使用指南
php中获取汉字拼音大写首字母
2021-07-05c /cad secondary development create arc (4)
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
【信息检索导论】第三章 容错式检索
view的绘制机制(三)
MySQL has no collation factor of order by
Analysis of MapReduce and yarn principles
MySQL composite index with or without ID
Cognitive science popularization of middle-aged people
RMAN incremental recovery example (1) - without unbacked archive logs
Alpha Beta Pruning in Adversarial Search