首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

PHP 截取汉语(UTF8版)

2012-10-26 
PHP 截取中文(UTF8版)function SubTitle($String,$Length) {?if (mb_strwidth($String, UTF8) $Lengt

PHP 截取中文(UTF8版)

function SubTitle($String,$Length) {
?if (mb_strwidth($String, 'UTF8') <= $Length ){
??return $String;
?}else{
??$I = 0;
??$len_word = 0;
??while ($len_word < $Length){
???$StringTMP = substr($String,$I,1);
???if ( ord($StringTMP) >=224 ){
????$StringTMP = substr($String,$I,3);
????$I = $I + 3;
????$len_word = $len_word + 2;
???}elseif( ord($StringTMP) >=192 ){
????$StringTMP = substr($String,$I,2);
????$I = $I + 2;
????$len_word = $len_word + 2;
???}else{
????$I = $I + 1;
????$len_word = $len_word + 1;
???}
???$StringLast[] = $StringTMP;
??}
??/* raywang edit it for dirk for (es/index.php)*/
??if (is_array($StringLast) && !empty($StringLast)){
???$StringLast = implode("",$StringLast);
???$StringLast .= "...";
??}
??return $StringLast;
?}
}

热点排行