This article mainly introduces the implementation of the PHP name according to the first letter sorting class and method, the code is easy to understand, very good, with a certain reference value, need to refer to the friend
PHP sorts the names alphabetically by the first letter, as shown in the following code:
<?phppublic function Getfirstchar ($s) {$s 0 = mb_substr ($s, 0, 3);//Get the first name $s = Iconv (' UTF-8 ', ' gb2312 ', $s 0);// Convert UTF-8 to GB2312 encoded//dump ($s 0); if (Ord ($s 0) >128) {//Kanji start, Chinese characters do not have $asc=ord ($s {0}) *256+ord ($s {1}) with the start of U, V, -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 <=-17760) return "G"; if ($asc >=-17759 and $asc <=-17248) return "H"; if ($asc >=-17247 and $asc <=-17418) return "I"; 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";} else if (ord ($s) >=48 and Ord ($s) <=57) {//number starts with switch (ICONV_SUBSTR ($s, 0,1, ' utf-8 ')) {case 1:return "Y"; Case 2: Return "E", Case 3:return "s", Case 4:return "s", Case 5:return "W", Case 6:return "L", Case 7:return "Q", Case 8:return "B"; CAs E 9:return "J"; case 0:return "L";}} else if (ord ($s) >=65 and Ord ($s) <=90) {//Capital English begins with return substr ($s, 0, 1);} else if (ord ($s) >=97 and Ord ($s) <=122) {//lowercase English begins with return Strtoupper (substr ($s, 0, 1));} Else{return Iconv_substr ($s 0,0,1, ' utf-8 ');//English mixed words are not suitable for all the above situations, so the first character can be extracted directly}}public function Addpeople () {$ UserName = Array (' Zhang San ', ' horse stud ', ' John Doe ', ' Harry ', ' Little Two ',' Cat eggs ', ' dog eggs ', ' crab ', ' Sanmao ', ' xiaoming ', ' Li Gang ', ' Zhang Fei '), sort ($userName), foreach ($userName as $name) {$char = $this->getfirstchar ($ Name), $nameArray = Array (), if (count ($charArray [$char])!=0) {$nameArray = $charArray [$char];} Array_push ($nameArray, $name); $charArray [$char] = $nameArray;} Ksort ($charArray); $this->assign (' _list ', $charArray);}
Page display
<foreach name= "_list" item= "Vo" key= "K" ><p>{$k}</p><foreach name= "vo" item= "L" ><span class= "Name" >{$l}</span></foreach></foreach>
Summarize
The above is a small part of the introduction of the PHP implementation of the name according to the first letter of the class and method, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of PHP Chinese network!
Articles you may be interested in:
PHP implementation of multi-image upload and single-image upload function php example
PHP SMS Verification Code implementation process detailed PHP example
PHP uses various functions to determine 0 and null PHP instances