PHP Substr庫(kù)函數(shù)的功能介紹
作者:佚名
這篇文章為大家介紹的是有關(guān)PHP Substr庫(kù)函數(shù)在處理中文中的具體功能體現(xiàn),希望對(duì)于初學(xué)PHP語(yǔ)言的朋友有所幫助。
初學(xué)PHP語(yǔ)言的朋友對(duì)于時(shí)常碰到的陌生函數(shù)都比較有興趣,一旦發(fā)現(xiàn)了這個(gè)函數(shù)的特殊功能后,往往都會(huì)很興奮。我們今天要給大家介紹的是關(guān)于PHP Substr庫(kù)函數(shù)的具體功能介紹。
#t#下面這個(gè)PHP Substr庫(kù)函數(shù)程序不算完美,但處理一般的中文(GB18030,GB2312,BIG5)是沒有問題的。這個(gè)函數(shù)不適合utf-8編碼的文字。
- //$str字符串
- //$max 最大字符數(shù)
- function Substring($str,$max){
- $cnt=0; //實(shí)際計(jì)數(shù)
- $index=0; //當(dāng)前索引
- $output=''; //輸出
- //
- while($cnt<$max && $index<strlen($str)){
- $output.=$str[$index];
- //big5
- if(ord($str[$index])>=0x81 &&
ord($str[$index])<=0xfe){- if($index+1<strlen($str)){
- if( (ord($str[$index+1])>=0x40
&& ord($str[$index+1])<0x7e)- || (ord($str[$index+1])>=0xa1
&& ord($str[$index+1])<=0xfe) ){- $index++;
- $output.=$str[$index];
- }
- }
- }
- //gb2312
- else if(ord($str[$index])>=0xa1
&& ord($str[$index])<=0xf7){- $output.=$str[$index];
- if($index+1<strlen($str)){
- if(ord($str[$index+1])>=0xa1
&& ord($str[$index+1])<0xfe){- $index++;
- $output.=$str[$index];
- }
- }
- }
- else{
- }
- $cnt++;
- $index++;
- }
- return $output;
- }
以上代碼示例就是PHP Substr庫(kù)函數(shù)在截取中文字符時(shí)的具體使用方法。
責(zé)任編輯:曹凱
來(lái)源:
CSDN