Example of getting the first letter of Chinese characters and pinyin in PHP

Source: Internet
Author: User
Tags ord

Let's take a look at how to get the first letter of a single Chinese character. See the following function. It supports GBK and UTF8 encoding:

The code is as follows: Copy code

Function getfirstchar ($ s0 ){
$ Fchar = ord ($ s0 {0 });
If ($ fchar> = ord ("A") and $ fchar <= ord ("z") return strtoupper ($ s0 {0 });
$ S1 = iconv ("UTF-8", "gb2312", $ s0 );
$ S2 = iconv ("gb2312", "UTF-8", $ s1 );
If ($ s2 = $ s0) {$ s = $ s1;} else {$ s = $ s0 ;}
$ Asc = ord ($ s {0}) * 256 + ord ($ s {1})-65536;
If ($ asc >=- 20319 and $ asc <=-20284) return "";
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;
}

The above function returns the first letter of a single Chinese character.

To process a Chinese string, you only need to write a new function to obtain the first letter of a string of Chinese characters.

The code is as follows: Copy code

Function pinyin1 ($ zh ){
$ Ret = "";
$ S1 = iconv ("UTF-8", "gb2312", $ zh );
$ S2 = iconv ("gb2312", "UTF-8", $ s1 );
If ($ s2 = $ zh) {$ zh = $ s1 ;}
For ($ I = 0; $ I <strlen ($ zh); $ I ++ ){
$ S1 = substr ($ zh, $ I, 1 );
$ P = ord ($ s1 );
If ($ p & gt; 160 ){
$ S2 = substr ($ zh, $ I ++, 2 );
$ Ret. = getfirstchar ($ s2 );
} Else {
$ Ret. = $ s1;
  }
 }
Return $ ret;
}

The above function is used to obtain the first letter of Chinese characters and pinyin. Example:

Echo pinyin1 ('This is a Chinese string ');

Result output: ZSZWZFC

Supplement inThe getfirstchar function can be written in two ways.

The first is the example we used above.

The code is as follows: Copy code

Function getfirstchar ($ s0 ){
$ Fchar = ord ($ s0 {0 });
If ($ fchar> = ord ("A") and $ fchar <= ord ("z") return strtoupper ($ s0 {0 });

The other is the numeric method we use, which is also relatively simple.

The code is as follows: Copy code

Function getFirstChar ($ string ){
$ FirstCharOrd = ord (strtoupper ($ string {0 }));
If ($ firstCharOrd >=65 & $ firstCharOrd <= 91) | ($ firstCharOrd >=48 & $ firstCharOrd <= 57 ))
Return strtoupper ($ string {0 });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.