如果在用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'); // 輸出7 中文英文都占一個字節
- mb_strlen($var); // 輸出15 中文占3個字節 英文占一個字節
- mb_strlen($var, 'gbk'); // 輸出9 不正常
取全部中文
- 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);
|
新聞熱點
疑難解答