100 to find the length of a test string
$str [0] = "123ASD hello [haha]"
$STR [1] = "123ASD Hello [Hello, not good]"
A Chinese character (utf-8 encoding) symbol [the word inside and the outer symbol is counted as a character]
Then calculate the length
The result is $str[0] and $str[1] for length 9
It's best to write a function. Thank you.
------Solution--------------------
I don't quite understand the landlord's question, but the function, there is one on hand.
PHP Code
/** * Get UTF8 length * @author mu_rain * @param String $str * @return Int */static function Strlen_utf 8 ($str) {$i = 0; $count = 0; $len = strlen ($STR); while ($i < $len) {$CHR = Ord ($str [$i]); $count + +; $i + +; if ($i >= $len) break; if ($CHR & 0x80) {$CHR <<= 1; while ($CHR & 0x80) {$i + +; $CHR <<= 1; }}} return $count; }
------Solution--------------------
PHP Code
function Mylen ($str, $startTag = ' [', $endTag = '] ', $encoding = ' utf-8 ') {$st = Preg_quote ($STARTTAG); $et = Preg_quote ($ENDTAG); Return Mb_strlen (Preg_replace ("#{$st}[^{$et}]*{$et}#", "~", $str), $encoding); }echo Mylen ("123asd hello [haha]");//9
------Solution--------------------
A bunch of string functions are attached and are used slowly.
PHP Code