当前位置:网站首页>php中获取汉字拼音大写首字母
php中获取汉字拼音大写首字母
2022-07-02 06:23:00 【夜空の雪風】
前言
在做项目开发的工程中遇到了要把一串中文字符串返回大写首字母的功能,在借鉴网上其他开发者的方法以后整理了一下最好用的一种方法。
问题描述
需要把【下游堆石区】转换成【XYDSQ】。
方法示例
if (!function_exists('getfirstchar')) {
/** * 获取汉字拼音大写首字母 * @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')) {
/** * 获取汉字拼音的大写首字母 * @param [type] $string * @return void */
function get_letter($string)
{
$charlist = preg_split('/(?<!^)(?!$)/u', (string) $string);
return implode(array_map("getfirstchar", $charlist));
}
}
使用方法
//【下游堆石区】转换成【XYDSQ】
$zone_name_pinyin = get_letter('下游堆石区');
边栏推荐
- SQLI-LABS通关(less1)
- CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現
- PXC high availability cluster summary
- Linux MySQL 5.6.51 Community Generic 安装教程
- 2021-07-17C#/CAD二次开发创建圆(5)
- A preliminary study on ant group G6
- Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
- Go package name
- In depth study of JVM bottom layer (V): class loading mechanism
- js创建一个自定义json数组
猜你喜欢
sqli-labs通關匯總-page2
UEditor .Net版本任意文件上传漏洞复现
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
sqli-labs通关汇总-page3
Pytest (2) mark function
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
js中map和forEach的用法
Latex error: the font size command \normalsize is not defined problem solved
Pytest (1) case collection rules
Apt command reports certificate error certificate verification failed: the certificate is not trusted
随机推荐
Latex compiles Chinese in vscode and solves the problem of using Chinese path
After reading useful blogs
Fe - wechat applet - Bluetooth ble development research and use
Utilisation de la carte et de foreach dans JS
查询GPU时无进程运行,但是显存却被占用了
(the 100th blog) written at the end of the second year of doctor's degree -20200818
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
sqli-labs通关汇总-page3
SQLI-LABS通关(less15-less17)
Uploading attachments using Win32 in Web Automation
There are multiple good constructors and room will problem
Blog directory of zzq -- updated on 20210601
How to try catch statements that return promise objects in JS
Record RDS troubleshooting once -- RDS capacity increases dramatically
Wechat applet Foundation
SQL injection closure judgment
uniapp引入本地字体
Sqli-labs customs clearance (less6-less14)
Basic knowledge of software testing
Solution to the black screen of win computer screenshot