当前位置:网站首页>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 ');
边栏推荐
- Conda 创建,复制,分享虚拟环境
- Oracle EBS ADI development steps
- Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
- Oracle apex 21.2 installation and one click deployment
- ORACLE EBS 和 APEX 集成登录及原理分析
- 在php的开发环境中如何调取WebService?
- sqli-labs通關匯總-page2
- 2021-07-05c /cad secondary development create arc (4)
- 使用Matlab实现:Jacobi、Gauss-Seidel迭代
- Data warehouse model fact table model design
猜你喜欢
随机推荐
【信息检索导论】第一章 布尔检索
ARP attack
Cognitive science popularization of middle-aged people
Oracle RMAN semi automatic recovery script restore phase
ORACLE EBS中消息队列fnd_msg_pub、fnd_message在PL/SQL中的应用
Oracle EBs and apex integrated login and principle analysis
php中获取汉字拼音大写首字母
Laravel8中的find_in_set、upsert的使用方法
MySQL组合索引加不加ID
Oracle EBS interface development - quick generation of JSON format data
ORACLE EBS 和 APEX 集成登录及原理分析
Use of interrupt()
叮咚,Redis OM对象映射框架来了
CSRF attack
Yolov5 practice: teach object detection by hand
php中判断版本号是否连续
TCP attack
How to efficiently develop a wechat applet
Sqli-labs customs clearance (less1)
Oracle rman半自动恢复脚本-restore阶段









