utf-8-关于PHP截取中英文字符串的几个问题
程序员文章站
2022-06-12 18:12:36
...
utf-8php
function substr_len($str, $len, $charset='utf-8'){
$len = intval($len);
if(!is_numeric($len) || $len 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 = $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;
}
下一篇: php的ci框架引入视图问题!