The code is as follows |
Copy Code |
Strlen int strlen (String $string) $str = ' 123456 '; echo strlen ($STR); 6 $STR = ' ab CD www.111cn.net '; echo strlen ($STR); 20 $ch = ' China '; echo strlen ($ch);//4 |
echo Utf8_strlen ($STR); 20
//
The code is as follows |
Copy Code |
function Utf8_strlen ($STR) { $count = 0; for ($i = 0; $i < strlen ($STR); $i + +) { $value = Ord ($str [$i]); if ($value > 127) { if ($value >= && $value <= 223) $i + +; ElseIf ($value >= 224 && $value <= 239) $i = $i + 2; ElseIf ($value >= && $value <= 247) $i = $i + 3; Else Die (' Not a UTF-8 compatible string '); }
$count + +; }
return $count; } |
Here's a look at the Mb_strlen example
echo ' <br/> ';
$STR = ' China ';
Echo Mb_strlen ($str, ' GBK '); 2 Length of 2, and just when we use strlen when the output of 4, from here can be seen Mb_strlen set his code for GBK when the Chinese processing.
Echo Mb_strlen ($str, ' utf8 ');//3
When we set the Mb_strlen parameter to be UFT8 encoded, his length was 3.
Take a look at the calculation of Chinese and English mixed-row string length instance
$str = ' Chinese a word 1 Chinese web first station www.111cn.net original article ';
Calculated as follows
Echo (strlen ($STR) + Mb_strlen ($str, ' UTF8 '))/2;
Echo
Output results
10
/*
Strlen//Calculate string length, one Chinese when 2 characters
Mb_strlen//According to its character encoding mode, statistic character quot;
In the use of mb_strlen you must take the PHP tutorial. ini php_mbstring.dll in front of the ";" Yes, but the general package is gone by default.
Original site article, reprinted annotated Source Www.111cn.net