当前位置:网站首页>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 ');
边栏推荐
- 【Torch】解决tensor参数有梯度,weight不更新的若干思路
- 类加载器及双亲委派机制
- Classloader and parental delegation mechanism
- Oracle apex 21.2 installation and one click deployment
- SSM二手交易网站
- JSP intelligent community property management system
- PM2 simple use and daemon
- MapReduce与YARN原理解析
- CSRF attack
- Sqli-labs customs clearance (less2-less5)
猜你喜欢
User login function: simple but difficult
Oracle EBs and apex integrated login and principle analysis
Illustration of etcd access in kubernetes
DNS attack details
【信息检索导论】第七章搜索系统中的评分计算
ssm垃圾分类管理系统
Ingress Controller 0.47.0的Yaml文件
Sqli labs customs clearance summary-page2
JSP intelligent community property management system
MySQL中的正则表达式
随机推荐
Oracle RMAN automatic recovery script (migration of production data to test)
腾讯机试题
类加载器及双亲委派机制
Practice and thinking of offline data warehouse and Bi development
Oracle EBS interface development - quick generation of JSON format data
数仓模型事实表模型设计
Explain in detail the process of realizing Chinese text classification by CNN
oracle apex ajax process + dy 校验
php中的数字金额转换大写数字
ssm人事管理系统
使用Matlab实现:Jacobi、Gauss-Seidel迭代
Spark SQL task performance optimization (basic)
Network security -- intrusion detection of emergency response
sparksql数据倾斜那些事儿
Ding Dong, here comes the redis om object mapping framework
Cognitive science popularization of middle-aged people
Oracle APEX 21.2 installation et déploiement en une seule touche
How to efficiently develop a wechat applet
搭建frp进行内网穿透
MySQL组合索引加不加ID