当前位置:网站首页>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('下游堆石区');
边栏推荐
- Basic knowledge of software testing
- JS judge whether the object is empty
- Solve the problem of bindchange event jitter of swiper component of wechat applet
- JS modification element attribute flipping commonly used in selenium's Web Automation
- Sqli labs customs clearance summary-page3
- Cve-2015-1635 (ms15-034) Remote Code Execution Vulnerability recurrence
- CTF web practice competition
- The table component specifies the concatenation parallel method
- 默认google浏览器打不开链接(点击超链接没有反应)
- Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
猜你喜欢

查询GPU时无进程运行,但是显存却被占用了

SQL注入闭合判断

Sqli labs customs clearance summary-page4

Wechat applet Foundation

2021-07-05C#/CAD二次开发创建圆弧(4)

The table component specifies the concatenation parallel method

PHP Session原理简析

Explanation and application of annotation and reflection

Solve the problem of bindchange event jitter of swiper component of wechat applet

Sqli - Labs Clearance (less6 - less14)
随机推荐
JS to determine whether there is a value in the object in the array
Deployment API_ automation_ Problems encountered during test
[literature reading and thought notes 13] unprocessing images for learned raw denoising
Pytest (1) case collection rules
JS divides an array into groups of three
js创建一个自定义json数组
CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現
PXC high availability cluster summary
AWD learning
Atcoder beginer contest 253 F - operations on a matrix / / tree array
Latex compilation error I found no \bibstyle &\bibdata &\citation command
Common function writing method and set get writing method for calculating attributes
Utilisation de la carte et de foreach dans JS
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
Sqli labs customs clearance summary-page4
Sqli labs customs clearance summary-page3
Win电脑截图黑屏解决办法
js删除字符串的最后一个字符
ZZQ的博客目录--更新于20210601
Win10: add or delete boot items, and add user-defined boot files to boot items