搜尋
首頁php教程php手册PHP获得中文汉字拼音首字母例子

获取给出汉字中拼音的第一个汉字字母我们可以利用汉字的一个编码来进行判断,下面我们来给大家介绍一个例子,非常的简单好用。

先来看看怎样取得单个汉字的拼音首字母,请看下面这个函数,它支持GBK和UTF8编码:

<?php
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 "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;
}
?>

 以上函数返回单个汉字的拼音首字母。

当需要处理中文字符串时,只需要重新写一个函数,用来取得一串汉字的拼音首字母。

<?php
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 > 160) {
            $s2 = substr($zh, $i++, 2);
            $ret.= getfirstchar($s2);
        } else {
            $ret.= $s1;
        }
    }
    return $ret;
}
?>

上面这个函数就是获取汉字拼音首字母的函数,使用示例:

echo pinyin1('这是中文字符串');

结果输出:ZSZWZFC

补充在 getfirstchar函数中我们有两种写法

第一种是我们上面用到的例子

<?php
function getfirstchar($s0) {
    $fchar = ord($s0{0});
    if ($fchar >= ord("A") and $fchar <= ord("z")) return strtoupper($s0{0});
?>
//而另一种是我们使用的数字方法了,也比较简单了。
<?php
    function getFirstChar($string) {
        $firstCharOrd = ord(strtoupper($string{0}));
        if (($firstCharOrd >= 65 && $firstCharOrd <= 91) || ($firstCharOrd >= 48 && $firstCharOrd <= 57)) return strtoupper($string{0});
}
?>


永久链接:

转载随意!带上文章地址吧。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能