Directly using PHP function substr intercept Chinese characters may appear garbled, mainly substr may abruptly a Chinese character "saw" into two halves. Workaround:
1, the use of mbstring Extension Library MB_SUBSTR interception will not appear garbled.
2, write the interception function, but the efficiency is not as high as the Mbstring expansion library.
3, if only for the output interception of the string, can be implemented as follows: substr ($STR, 0,). chr (0).
=============================
SUBSTR () function can split the text, but the text to be divided if the inclusion of Chinese characters tend to encounter problems, you can use MB_SUBSTR ()/mb_strcut This function, Mb_substr ()/mb_strcut usage and substr () similar, Just in Mb_substr ()/mb_strcut to add more than one parameter to set the string encoding, but the general server did not open Php_mbstring.dll, need to php.ini in the Php_mbstring.dll to open.
As an example:
<?php
Echo mb_substr (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>
Output: so that my word
<?php
Echo mb_strcut (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>
Output: Such a
As can be seen from the above example, Mb_substr is to divide characters by word, while mb_strcut splits characters by Byte, but does not produce a half-character phenomenon.
=============================
A method of text string interception without garbled characters in PHP implementation
function Gbsubstr ($string, $start, $length) {
if (strlen ($string) > $length) {
$str =null;
$len = $start + $length;
for ($i = $start; $i < $len; $i + +) {
if (Ord (substr ($string, $i, 1)) >0xa0) {
$str. =substr ($string, $i, 2);
$i + +;
}else{
$str. =substr ($string, $i, 1);
}
}
return $str. ' ... ';
}else{
return $string;
}
}
A method of text string interception without garbled characters in PHP implementation