欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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;

}

相关标签: utf-8 php