截取中文字符串
function substrUTF8($string, $start, $length = null) {
$len = strlen($string);
if($len <= 0){
return '';
}
if($length == null){
$length = $len;
}
if($start < 0 || $length <= 0){
return '';
}
//找到start的位置
$pos = 0;//字节位置
$position = 0;//实际位置
$str = null;
if($start > 0){
for($i = 0 ; $i < $len ; $i++){
$position = $position + 1;
if( ord(substr($string,$i,1)) > 0xa0){
$i++;$i++;
$pos = $i + 1;
}else{
$pos = $i + 1;
}
if($position == $start){
break;
}
}
}
if($pos >= $len){
return '';
}
for($j = $pos ; $j < $len ; $j++){
if( ord(substr($string,$j,1)) > 0xa0){
$str .= substr($string,$j,3);
$j++;$j++;
$pos = $j + 1;
}else{
$str .= substr($string,$j,1);
$pos = $j + 1;
}
if($pos == $len){
break;
}
$length--;
if($length == 0){
break;
}
}
return $str;
}
//GBK的类似
截取中文字符串
最新推荐文章于 2019-07-22 20:20:58 发布