search

Home  >  Q&A  >  body text

php - Count Chinese characters in a string

$str = '欧耶123';
//中文字节
$zl = strlen($str) - mb_strlen($str,'utf8');
//英文字节
$yl = mb_strlen($str, 'utf8') - $zl / 2;

中文个数 
$znum = $zl /2;

Can’t figure out why it’s calculated like this?

某草草某草草2714 days ago701

reply all(1)I'll reply

  • 阿神

    阿神2017-06-27 09:20:04

    mb_substr()中文一个字算3个字节,截取中文设置参数utf-8
    
    mb_strlen($str, 'utf-8'),每个中文一个
    mb_strlen($str, 'gb2312'),每个中文2个

    reply
    0
  • Cancelreply