Home > Article > Backend Development > PHP Chinese string interception function_PHP tutorial
The following two functions are two double-byte string interception functions, which are designed to intercept Chinese character strings. The first Chinese character interception function is more concise than before, and the latter one is more complex but requires more consideration.
//php tutorial Chinese string interception function
/*
The following two functions are two double-byte string interception functions, which are for Chinese string interception , well, the first Chinese character interception function is more concise, and the latter one is more complicated but requires more consideration.
*/
function substr($str = '', $offset = 0, $len = 0){
$len || ($len = strlen($str));
preg_match_all('/./us', $str, $result);
return implode('', array_slice($result[0], $offset, $len));
}
//Method 2
if (!function_exists('mb_substr')) {
function mb_substr($str, $start, $len = '', $encoding="utf -8"){
$limit = strlen($str);for ($s = 0; $start > 0;--$start) {// found the real start
if ($s >= $limit)
break;if ($str[$s] <= "x7f")
++$s;
else {
++$s; // skip lengthwhile ($str[$s] >= "x80" && $str[$s] <= "xbf")
+ +$s;
}
}if ($len == '')
return substr($str, $s);
else
for ($ e = $s; $len > 0; --$len) {//found the real end
if ($e >= $limit)
break;if ($ str[$e] <= "x7f")
++$e;
else {
++$e;//skip lengthwhile ($str[$e ] >= "x80" && $str[$e] <= "xbf" && $e < $limit)
++$e; return substr($str, $s, $e - $s);
}
}
?>
http://www.bkjia.com/PHPjc/444857.htmlwww.bkjia.com