PHP自定義函數(shù)獲取漢字首字母的方法
本文實(shí)例講述了PHP自定義函數(shù)獲取漢字首字母的方法。分享給大家供大家參考,具體如下:
首字母很重要,可以進(jìn)行排序使用。
城市列表等等。
<?php /* * Created on 2016-12-1 */ function getFirstCharter($str) { if (empty($str)) { return ''; } $fchar = ord($str{0}); if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0}); $s1 = iconv('UTF-8', 'gb2312', $str); $s2 = iconv('gb2312', 'UTF-8', $s1); $s = $s2 == $str ? $s1 : $str; $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; if ($asc >= -20319 && $asc <= -20284) return 'A'; if ($asc >= -20283 && $asc <= -19776) return 'B'; if ($asc >= -19775 && $asc <= -19219) return 'C'; if ($asc >= -19218 && $asc <= -18711) return 'D'; if ($asc >= -18710 && $asc <= -18527) return 'E'; if ($asc >= -18526 && $asc <= -18240) return 'F'; if ($asc >= -18239 && $asc <= -17923) return 'G'; if ($asc >= -17922 && $asc <= -17418) return 'H'; if ($asc >= -17417 && $asc <= -16475) return 'J'; if ($asc >= -16474 && $asc <= -16213) return 'K'; if ($asc >= -16212 && $asc <= -15641) return 'L'; if ($asc >= -15640 && $asc <= -15166) return 'M'; if ($asc >= -15165 && $asc <= -14923) return 'N'; if ($asc >= -14922 && $asc <= -14915) return 'O'; if ($asc >= -14914 && $asc <= -14631) return 'P'; if ($asc >= -14630 && $asc <= -14150) return 'Q'; if ($asc >= -14149 && $asc <= -14091) return 'R'; if ($asc >= -14090 && $asc <= -13319) return 'S'; if ($asc >= -13318 && $asc <= -12839) return 'T'; if ($asc >= -12838 && $asc <= -12557) return 'W'; if ($asc >= -12556 && $asc <= -11848) return 'X'; if ($asc >= -11847 && $asc <= -11056) return 'Y'; if ($asc >= -11055 && $asc <= -10247) return 'Z'; return null; } $firstChar = getFirstCharter('腳本之家'); print_r($firstChar);//輸出:J ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php常用函數(shù)與技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP中文處理 中文字符串截取(mb_substr)和獲取中文字符串字?jǐn)?shù)
PHP中文處理 中文字符串截取(mb_substr)和獲取中文字符串字?jǐn)?shù),需要的朋友可以參考下。2011-11-11php中mail函數(shù)發(fā)送郵件失敗的解決方法
這篇文章主要介紹了php中mail函數(shù)發(fā)送郵件失敗的解決方法,涉及針對Linux運(yùn)行平臺(tái)相關(guān)組件的配置技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08PHP實(shí)現(xiàn)批量檢測網(wǎng)站是否能夠正常打開的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)批量檢測網(wǎng)站是否能夠正常打開的方法,涉及php使用自定義函數(shù)及curl操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08PHP 7安裝調(diào)試工具Xdebug擴(kuò)展的方法教程
Xdebug提供了豐富的調(diào)試函數(shù),也可將Xdebug安裝配置為zend studio、editplus調(diào)試PHP的第三方插件,通過開啟自動(dòng)跟蹤(auto_trace)和分析器功能,可以直觀的看到PHP源代碼的性能數(shù)據(jù),以便優(yōu)化PHP代碼,本文介紹了PHP 7安裝調(diào)試工具Xdebug擴(kuò)展的方法,需要的朋友可以參考。2017-06-06