UTF-8 's Chinese string is three bytes
Copy Code code as follows:
<?php
Coding UTF-8
echo strlen (' Test text a Test text ');
Echo '-';
echo mb_strlen (' Test text a Test text ', ' utf-8 ');
?>
Output: 25-9
GB2312 's Chinese string is two bytes
Copy Code code as follows:
<?php
Coding GB2312
echo strlen (' Test text a Test text ');
Echo '-';
echo mb_strlen (' Test text a Test text ', ' Gb2312 ');
?>
Output: 17-9
In the MySQL database (later version 5.1), you can insert 10 characters (not bytes) If the field type is varchar (10);
Therefore, it is necessary to distinguish the length of the string according to the document encoding.
Character a simple UTF-8 string interception (by the number of characters to intercept)
Copy Code code as follows:
?
/*
* UTF-8 string Intercept
* $str the string to intercept
* $start intercept start position
* $length Intercept length
*/
function Cutstr ($str, $start, $length) {
$restr = ';
$j = 0;
$end = $length + $start-1;
$plen = strlen ($STR);
for ($i =0; $i < $plen; $i + +) {
$restr. = Ord ($str [$i]) >127? $str [$i]. $str [+ + $i]. $str [+ + $i]: $str [$i];
$j + +;
if ($j < $start) {$restr = ';}
if ($j >= $end) {break;}
}
$restr. = ';
return $restr;
}
$str = ' Beijing, September 24 electric Power Group of 20 (G20) leaders of the third financial summit will be held in the United States Pittsburg today. ';
Echo $str;
Echo ' <br> ';
Echo utf8_substr ($str, 0,25);
Echo ' <br> ';
?>