当前位置:Gxlcms > PHP教程 > utf-8-关于PHP截取中英文字符串的几个问题

utf-8-关于PHP截取中英文字符串的几个问题

时间:2021-07-01 10:21:17 帮助过:18人阅读

utf-8php

function substr_len($str, $len, $charset='utf-8'){
$len = intval($len);
if(!is_numeric($len) || $len < 0){
return false;
}

$str_len = strlen($str);if($len >= $str_len){    return $str;}if($charset == 'utf-8'){    $char_step = 3;}else{    $char_step = 2;}$substr_i = 0;//截取长度$act_substr = 0;for($i = 0; $i < $str_len; $i++){    if($substr_i >= $len){        break;    }    $sub_val = substr($str, $i, 1);    if(ord($sub_val) > 0xa0){        $i += $char_step - 1;        $act_substr += $char_step;    }else{        $act_substr ++;    }    $substr_i++;}$result_str = substr($str, 0, $act_substr);return $result_str;

}

人气教程排行