/**作用:统计字符长度包括中文、英文、数字
* 参数:需要进行统计的字符串、编码格式目前系统统一使用UTF-8
* 修改记录:
$str = "kds";
echo sstrlen($str,'utf-8');
* */
function sstrlen($str,$charset) {
$n = 0; $p = 0; $c = '';
$len = strlen($str);
if($charset == 'utf-8') {
for($i = 0; $i < $len; $i++) {
$c = ord($str{$i});
if($c > 252) {
$p = 5;
} elseif($c > 248) {
$p = 4;
} elseif($c > 240) {
$p = 3;
} elseif($c > 224) {
$p = 2;
} elseif($c > 192) {
$p = 1;
} else {
$p = 0;
}
$i+=$p;$n++;
}
} else {
for($i = 0; $i < $len; $i++) {
$c = ord($str{$i});
if($c > 127) {
$p = 1;
} else {
$p = 0;
}
$i+=$p;$n++;
}
}
return $n;
}

相关文章:

  • 2021-08-31
  • 2022-12-23
  • 2021-07-16
  • 2021-11-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2021-09-30
相关资源
相似解决方案