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基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php獲取漢字首字母的函數(shù)
- 利用PHP獲取漢字首字母并且分組排序詳解
- php 獲得漢字拼音首字母的函數(shù)
- 使用PHP獲取漢字的拼音(全部與首字母)
- php獲取漢字拼音首字母的方法
- PHP實(shí)現(xiàn)將漢字轉(zhuǎn)換為拼音及獲取詞語(yǔ)首字母的方法
- PHP基于ICU擴(kuò)展intl快速實(shí)現(xiàn)漢字轉(zhuǎn)拼音及按拼音首字母分組排序的方法
- 基于PHP如何把漢字轉(zhuǎn)化為拼音
- php中將漢字轉(zhuǎn)換成拼音的函數(shù)代碼
- PHP漢字轉(zhuǎn)換拼音的函數(shù)代碼
- PHP常用函數(shù)之獲取漢字首字母功能示例
相關(guān)文章
利用PHP擴(kuò)展Xhprof分析項(xiàng)目性能實(shí)踐教程
XHProf是Facebook開(kāi)發(fā)的性能調(diào)試工具,能幫助直觀的統(tǒng)計(jì)顯示PHP程序執(zhí)行中各方法函數(shù)調(diào)用次數(shù)和消耗時(shí)間,以方便我們排查性能瓶頸并進(jìn)行調(diào)優(yōu)。下面這篇文章主要給大家介紹了關(guān)于利用PHP擴(kuò)展Xhprof分析項(xiàng)目性能實(shí)踐的相關(guān)資料,需要的朋友可以參考下2018-09-09
Linux系統(tǒng)中為php添加pcntl擴(kuò)展
上篇文章我們介紹了在mac系統(tǒng)中為片php添加pcntl擴(kuò)展插件,本文我們繼續(xù)來(lái)分享下Linux系統(tǒng)中為php添加pcntl擴(kuò)展的方法,希望小伙伴們能夠喜歡2016-08-08
Search File Contents PHP 搜索目錄文本內(nèi)容的代碼
這個(gè)類可以用來(lái)搜索在給定的文本目錄中的文件。它可以給定目錄遍歷遞歸查找某些文件擴(kuò)展名的文件。2010-02-02
PHP實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)簡(jiǎn)單爬蟲(chóng)的方法,涉及php頁(yè)面抓取及分析的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
PHP實(shí)現(xiàn)的多維數(shù)組去重操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的多維數(shù)組去重操作,結(jié)合實(shí)例形式分析了php序列化json字符串及數(shù)組轉(zhuǎn)換、去重等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
PHP自動(dòng)生成后臺(tái)導(dǎo)航網(wǎng)址的最佳方法
寫(xiě)過(guò)很多種不同的后臺(tái)導(dǎo)航寫(xiě)法最終積累了這種最寫(xiě)法,算是最好的吧,個(gè)人感覺(jué)還算是不錯(cuò)的,喜歡的朋友可以參考下2013-08-08
php簡(jiǎn)單靜態(tài)頁(yè)生成過(guò)程
一直用smarty的cache,但感覺(jué)還是要自己做一個(gè),才有感覺(jué)。網(wǎng)上有很多牛人的功能比較完備,打算先自己搞簡(jiǎn)單的再慢慢豐滿。這兩天做了一個(gè)比較簡(jiǎn)單的,在hi.baidu.net/alex_wang58記錄一下。2008-03-03

