Understand PHP are aware of strlen and Mb_strlen is the string length of the function, but for some beginners, if not read the manual, perhaps not quite clear the difference.
Understand PHP are aware of strlen and Mb_strlen is the string length of the function, but for some beginners, if not read the manual, perhaps not quite clear the difference.
Look at this piece of code first (the prerequisite: character encoding is UTF-8):
?
$str = ' understand PHP know strlen and Mb_strlen is the function of string length ';
echo strlen ($STR) '. <br/> '. Mb_strlen ($str, ' utf-8 ');
?>
Run the above code and return the following values:
66
34
What do you think? In Strlen, Chinese is three byte length, English is the length of a byte! In Mb_strlen, are counted as a byte of length! Therefore, we sometimes use substr to intercept UTF-8 Chinese strings, often garbled, is the reason!
A function that intercepts the UTF-8 string is provided below:
function Cutstr ($SOURCESTR, $cutlength) {
$returnstr = ';
$i = 0;
$n = 0;
$str _length = strlen ($SOURCESTR);
$MB _str_length = Mb_strlen ($sourcestr, ' utf-8 ');
while ($n < $cutlength) && ($i <= $str _length)) {
$temp _str = substr ($sourcestr, $i, 1);
$ascnum = Ord ($temp _str);
if ($ascnum >= 224) {
$returnstr = $returnstr. substr ($sourcestr, $i, 3);
$i = $i + 3;
$n + +;
}
ElseIf ($ascnum >= 192) {
$returnstr = $returnstr. substr ($sourcestr, $i, 2);
$i = $i + 2;
$n + +;
}
ElseIf ($ascnum >=) && ($ascnum <= 90)) {
$returnstr = $returnstr. substr ($sourcestr, $i, 1);
$i = $i + 1;
$n + +;
}
else{
$returnstr = $returnstr. substr ($sourcestr, $i, 1);
$i = $i + 1;
$n = $n + 0.5;
}
}
if ($MB _str_length > $cutlength) {
$returnstr = $returnstr. "...";
}
return $returnstr;
}
Use examples:
?
$STR = ' Valid for a maximum period of three months, the system will automatically delete this article information ';
echo strlen ($STR);
echo ' echo ' echo ' ?>