The Chinese string of UTF-8 is three bytes
Copy code The code is as follows:
//Encoding UTF-8
echo strlen('test text a test text');
echo '-';
echo mb_strlen('test text a test text', 'utf-8');
?>
Output: 25-9
The Chinese string of GB2312 is two bytes
Copy code The code is as follows:
//Encoding GB2312
echo strlen('Test text a test text');
echo '-';
echo mb_strlen('test text a test text','Gb2312');
?>
Output: 17- 9
In Mysql database (versions after 5.1), if the field type is varchar(10), 10 characters (not bytes) can be inserted;
So when judging the length of the string, you need to encode it according to the document to distinguish.
symbol is a simple string interception under UTF-8 (interception based on the number of characters)
Copy code The code is as follows:
/*
* UTF-8 string interception
* $str String to intercept
* $start interception starting position
* $length interception 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 = 'China News Service, September 24th. The third financial summit of leaders of the Group of Twenty (G20) will be held in Pittsburgh, USA today. ';
echo $str;
echo '
';
echo utf8_substr($str,0,25);
echo '
';
?> ;
http://www.bkjia.com/PHPjc/320670.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320670.htmlTechArticleThe Chinese string of UTF-8 is three bytes. Copy the code as follows: ?php //Encoding UTF- 8 echo strlen('test text a test text'); echo '-'; echo mb_strlen('test text a test text',...