mb_string有的服務器沒有編譯進去。
下邊有壹個截取中文字符的函數,要求是GBK編碼的。
$str是字符串,$strlen是要截取的長度,壹個中文算兩個字符。
用這個函數就不會出現亂碼了
<?php
/* 截取壹定長度的完整的中文字符 */
function cnsubstr($str,$strlen=10) {
if(empty($str)||!is_numeric($strlen)){
return false;
}
if(strlen($str)<=$strlen){
return $str;
}
//得到第$length個字符 並判斷是否為非中文 若為非中文
//直接返回$length長的字符串
$last_word_needed=substr($str,$strlen-1,1);
if(!ord($last_word_needed)>128){
$needed_sub_sentence=substr($str,0,$strlen);
return $needed_sub_sentence;
}else{
for($i=0;$i<$strlen;$i++){
if(ord($str[$i])>128){
$i++;
}
}//end of for
$needed_sub_sentence=substr($str,0,$i);
return $needed_sub_sentence;
}
}