Home php教程 php手册 php获取中文字符拼音首字母实例

php获取中文字符拼音首字母实例

May 25, 2016 pm 04:44 PM
Chinese characters Initials

在php中如果我们要获取汉字的拼音第一个字母我们先要了解asc码的范围码了,我们只要知道这个值上下限就可以使用php中的ord来获取我相关的汉字拼音了.

实例1,代码如下:

function getFirstCharter($str) { 
    if (emptyempty($str)) {return '';} 
    $fchar = ord($str{0}); 
    if ($fchar>=ord(&#39;A&#39;) && $fchar<=ord(&#39;z&#39;)) return strtoupper($str{0}); 
    $s1 = iconv(&#39;UTF-8&#39;, &#39;gb2312&#39;, $str); 
    $s2 = iconv(&#39;gb2312&#39;, &#39;UTF-8&#39;, $s1); 
    $s = $s2 == $str ? $s1 : $str; 
    $asc = ord($s{0})*256 + ord($s{1}) - 65536; 
    if ($asc>=-20319 && $asc<=-20284) return &#39;A&#39;; 
    if ($asc>=-20283 && $asc<=-19776) return &#39;B&#39;; 
    if ($asc>=-19775 && $asc<=-19219) return &#39;C&#39;; 
    if ($asc>=-19218 && $asc<=-18711) return &#39;D&#39;; 
    if ($asc>=-18710 && $asc<=-18527) return &#39;E&#39;; 
    if ($asc>=-18526 && $asc<=-18240) return &#39;F&#39;; 
    if ($asc>=-18239 && $asc<=-17923) return &#39;G&#39;; 
    if ($asc>=-17922 && $asc<=-17418) return &#39;H&#39;; 
    if ($asc>=-17417 && $asc<=-16475) return &#39;J&#39;; 
    if ($asc>=-16474 && $asc<=-16213) return &#39;K&#39;; 
    if ($asc>=-16212 && $asc<=-15641) return &#39;L&#39;; 
    if ($asc>=-15640 && $asc<=-15166) return &#39;M&#39;; 
    if ($asc>=-15165 && $asc<=-14923) return &#39;N&#39;; 
    if ($asc>=-14922 && $asc<=-14915) return &#39;O&#39;; 
    if ($asc>=-14914 && $asc<=-14631) return &#39;P&#39;; 
    if ($asc>=-14630 && $asc<=-14150) return &#39;Q&#39;; 
    if ($asc>=-14149 && $asc<=-14091) return &#39;R&#39;; 
    if ($asc>=-14090 && $asc<=-13319) return &#39;S&#39;; 
    if ($asc>=-13318 && $asc<=-12839) return &#39;T&#39;; 
    if ($asc>=-12838 && $asc<=-12557) return &#39;W&#39;; 
    if ($asc>=-12556 && $asc<=-11848) return &#39;X&#39;; 
    if ($asc>=-11847 && $asc<=-11056) return &#39;Y&#39;; 
    if ($asc>=-11055 && $asc<=-10247) return &#39;Z&#39;; 
    return null; 
}
Copy after login

例如:echo getFirstCharter("程序员3aj.cn"); // 结果将输出:C

实例二,代码如下:

<?php 
 
$limit=array( //gb2312 拼音排序 
    array(45217,45252), //A 
    array(45253,45760), //B 
    array(45761,46317), //C 
    array(46318,46825), //D 
    array(46826,47009), //E 
    array(47010,47296), //F 
    array(47297,47613), //G 
    array(47614,48118), //H 
    array(0,0),         //I 
    array(48119,49061), //J 
    array(49062,49323), //K 
    array(49324,49895), //L 
    array(49896,50370), //M 
    array(50371,50613), //N 
    array(50614,50621), //O 
    array(50622,50905), //P 
    array(50906,51386), //Q 
    array(51387,51445), //R 
    array(51446,52217), //S 
    array(52218,52697), //T 
    array(0,0),         //U 
    array(0,0),         //V 
    array(52698,52979), //W 
    array(52980,53688), //X 
    array(53689,54480), //Y 
    array(54481,55289), //Z 
); 
 
$str="A:这是一个测试程序1"; 
$str= iconv("UTF-8","gb2312", $str); 
echo $str."</br>"; 
$i=0; 
while($i<strlen($str) ) { 
    $tmp=bin2hex(substr($str,$i,1)); 
    if($tmp>=&#39;B0&#39;){ //汉字的开始 
        $t=getLetter(hexdec(bin2hex(substr($str,$i,2)))); 
        printf("%c",$t==-1 ? &#39;*&#39; : $t ); 
        $i+=2; 
    } 
    else{ 
        printf("%s",substr($str,$i,1)); 
        $i++; 
    } 
} 
 
function getLetter($num){ 
    global $limit; 
    $char_index=65; 
    foreach($limit as $k=>$v){ 
        if($num>=$v[0] && $num<=$v[1]){ 
            $char_index+=$k; 
            return $char_index; 
        } 
    } 
    return -1; 
}
Copy after login


教程网址:

欢迎收藏∩_∩但请保留本文链接。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1247
24
How to deal with the pinyin sorting problem of Chinese characters in PHP? How to deal with the pinyin sorting problem of Chinese characters in PHP? Sep 05, 2023 pm 05:00 PM

How to deal with the pinyin sorting problem of Chinese characters in PHP? When developing Chinese websites or applications, we often face the need to sort Chinese strings according to pinyin. However, due to the complexity of Chinese characters, directly using conventional sorting algorithms will lead to errors in sorting results. Therefore, we need to use a special method to deal with the pinyin sorting problem of Chinese characters. In PHP, a common solution is to use a pinyin library such as "Overtrue/Pinyin". This is a pinyin based on PHP

How to verify whether the input is all Chinese characters in golang How to verify whether the input is all Chinese characters in golang Jun 24, 2023 am 09:16 AM

With the development of the times, we pay more and more attention to the verification of data, especially the verification of user input. For language verification, how to accurately determine whether the input is all Chinese characters has become an important issue. In golang, we can use the unicode package and regexp package to achieve this requirement. 1. Unicode package The unicode package provides a series of core support for Unicode. We can use the functions in this package to accurately determine whether a character is a Chinese character.

How to sort Chinese characters in C language environment? How to sort Chinese characters in C language environment? Feb 18, 2024 pm 02:10 PM

How to implement Chinese character sorting function in C language programming software? In modern society, the Chinese character sorting function is one of the essential functions in many software. Whether in word processing software, search engines or database systems, Chinese characters need to be sorted to better display and process Chinese text data. In C language programming, how to implement the Chinese character sorting function? One method is briefly introduced below. First of all, in order to implement the Chinese character sorting function in C language, we need to use the string comparison function. Ran

How to use regular expressions to match Chinese characters in PHP How to use regular expressions to match Chinese characters in PHP Jun 22, 2023 am 09:16 AM

In PHP, regular expression is a commonly used string matching tool. It can be used to determine whether a string conforms to a specific format, thereby verifying the validity of the input value. When processing Chinese characters, since Chinese characters and English characters are encoded differently, the matching rules of the regular expression need to be adjusted accordingly. This article will introduce how to use regular expressions to match Chinese characters in PHP. 1. Understand Chinese character encoding. Commonly used character encodings in PHP are UTF-8 and G.

How to get the first letter of Chinese characters in PHP? How to get the first letter of Chinese characters in PHP? Sep 06, 2023 am 11:18 AM

How to get the first letter of Chinese characters in PHP? When processing Chinese characters, sometimes we need to get the first letter of the Chinese character. PHP provides some built-in functions and extension packages to achieve this functionality. A common way is to use the mb_substr() function in combination with the ord() function. The mb_substr() function is used to obtain the substring of a string, and the ord() function is used to obtain the ASCII code value of a character. We can get the first letter of the Chinese character by getting the first character and its ASCII code value.

How to implement a search function in PHP that converts a Chinese character into the first letter of Pinyin? How to implement a search function in PHP that converts a Chinese character into the first letter of Pinyin? Sep 06, 2023 am 09:00 AM

How to implement a search function in PHP that converts a Chinese character into the first letter of Pinyin? In many web applications, we often need to implement search functions. One of the very common requirements is to implement a search function that converts Chinese characters into the first letter of Pinyin. In this way, users can search for relevant content by entering the first letter of Chinese pinyin, improving the accuracy and convenience of search. To implement the search function of converting Chinese characters into Pinyin initials in PHP, you can use a third-party library to implement it. One of the commonly used libraries is PHP

An effective method to solve the problem of Chinese garbled characters in PHP An effective method to solve the problem of Chinese garbled characters in PHP Mar 07, 2024 pm 09:30 PM

An effective method to solve the problem of Chinese garbled characters in PHP. During the development of PHP, we often encounter the problem of Chinese garbled characters, which brings inconvenience to our program development and data processing. The problem of Chinese garbled characters usually occurs because the character encoding is not uniform or there is a character set conversion error. This article will introduce some effective methods to solve the problem of Chinese garbled characters in PHP and give specific code examples. 1. Set the PHP file encoding format. First, make sure the encoding format is clearly specified in the PHP file. You can usually add the following at the beginning of the file.

PHP regular expression practice: matching Chinese characters PHP regular expression practice: matching Chinese characters Jun 22, 2023 pm 08:34 PM

In the process of developing projects using PHP, we often encounter the need to process Chinese characters. Regular expressions are a powerful text processing tool that can help us match and process Chinese characters quickly and accurately. In this article, I will introduce related techniques and examples on how to use PHP regular expressions to match Chinese characters. Matching Chinese Characters First, we need to understand how Chinese characters are represented in the computer. Normally, Chinese characters are represented using Unicode encoding. In Unicod

See all articles