中文字符串函数php,php截取中文字符串函数实例,_PHP教程
php截取中文字符串函數(shù)實例,
本文實例講述了php截取中文字符串函數(shù)。分享給大家供大家參考。具體實現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
//中文字符串截取
function substr_zh($string,$sublen,$start=0,$code='UTF-8'){
if($code=='UTF-8'){
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa,$string,$t_string);
if(count($t_string[0])-$start > $sublen){
return join('',array_slice($t_string[0],$start,$sublen))."...";
//array_slice()在數(shù)組中根據(jù)條件取出一段值,參數(shù)(數(shù)組,開始位置,[長度])
}else{
return join('',array_slice($t_string[0],$start,$sublen));
}
}else{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0;$i
if($i>$start && $i
if(ord(substr($string,$i,1))>129){
//ord():返回字符串第一個字符的ASCII值
//substr():返回字符串的一部分
$tmpstr .= substr($string,$i,2);
}else{
$tmpstr .= substr($string,$i,1);
}
}
if(ord(substr($string,$i,1))>129){
$i++;
}
if(strlen($tmpstr)
$tmpstr .= "...";
}
}
return $tmpstr;
}
}
$string ="頂置車頂起困境檲上盯協(xié)押畏奇才趄肯困楞右腳可愛有";
echo substr_zh($string,10,0,'gb2312');
?>
希望本文所述對大家的php程序設(shè)計有所幫助。
http://www.bkjia.com/PHPjc/959883.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/959883.htmlTechArticlephp截取中文字符串函數(shù)實例, 本文實例講述了php截取中文字符串函數(shù)。分享給大家供大家參考。具體實現(xiàn)方法如下: 復(fù)制代碼 代碼如下...
總結(jié)
以上是生活随笔為你收集整理的中文字符串函数php,php截取中文字符串函数实例,_PHP教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一分硬币发行年份
- 下一篇: jsp连接mysql显示404,Spri