Parse PHP to get the pinyin initials of the title (with code examples)

04-08-2024

This article brings you some knowledge about php, which mainly introduces how php obtains the pinyin initials of the title. There are code examples. Let's take a look at them with interested friends. I hope it will help you.

Get the pinyin initials of the title

It should be noted here that the interval of ascii value of X may be judged to be empty, and it has been modified.

First, compare the ascii code value of letters by calculating the ascii code value.

public function getfirstchar($s0) { $fchar = ord($s0{0}); if($fchar >= ord("A") and $fchar = -20319 and $asc = -20283 and $asc = -19775 and $asc = -19218 and $asc = -18710 and $asc = -18526 and $asc = -18239 and $asc = -17922 and $asc = -17922 and $asc = -17417 and $asc = -16474 and $asc = -16212 and $asc = -15640 and $asc = -15165 and $asc = -14922 and $asc = -14914 and $asc = -14630 and $ ASC =-14149 and $ ASC =-14090 and $ ASC =-13318 and $ ASC =-12838 and $ ASC =-22605 and $ ASC =-11847 and $ ASC =-11055 and $ ASC II. Get the pinyin initials of the whole title

public function pinyin_long($zh) { $ret = ""; if(empty($zh)) { $ret="x"; }else{ $s1 = mb_convert_encoding($zh,"GBK","UTF-8"); $s2 = mb_convert_encoding($s1,"UTF-8","GBK"); if($s2 == $zh){$zh = $s1; } for($i = 0; $i < strlen($zh); $i++){ $s1 = substr($zh,$i,1); $p = ord($s1); if($p > 160){ $s2 = substr($zh,$i++,2); $ret .= $this->getfirstchar($s2); }else{ $ret .= $s1; } } } return $ret; }

Recommended study: PHP video tutorial

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us