如果在用mb_strlen出现fatal error: call to undefined function mb_strlen,这种问题你要可以用php info()看一下有没有装载mbstring,如果没有,尝试将php_mbstring.dll复制到windows目录下。
文件编码 utf-8
- $var = '中文字符abc';
- mb_strlen($var, 'utf-8');
- mb_strlen($var);
- mb_strlen($var, 'gbk');
取全部中文
- function utf8substr($str, $from, $len)
- {
- return preg_replace('#^(?:[x00-x7f]|[xc0-xff][x80-xbf]+){0,'.$from.'}'.
- '((?:[x00-x7f]|[xc0-xff][x80-xbf]+){0,'.$len.'}).*#s',
- '$1',$str);
- }
中文与英混体截取代码
- function gb2312_strlen($string)
- {
- $str_len = strlen($string);
- $str_count = 0;
- for($j = 0; $j < $str_len; $j++)
- {
- if(ord($string{$j}) < 127)
- {
- $str_count += 1;
- continue;
- }
- else
- {
- if(ord($string{$j+1}) > 127)
- {
- $str_count += 1;
- $j++;
- continue;
- }
- else
- {
- $str_count += 1;
- continue;
- }
- }
- }
- return $str_count;
- }
- $str = "开s d";
- echo gb2312_strlen($str);