The following two functions are two double-byte string interception function, that is, for the Chinese string interception, good first Chinese character interception function is leapfrog concise, the latter a complex but consider more.
PHP Tutorial Chinese string intercept function
/*
The following two functions are two double-byte string interception function, that is, for the Chinese string interception, good first Chinese character interception function is leapfrog concise, the latter a complex but consider more.
*/
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 Two
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 length
while ($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 length
while ($str [$e] >= "x80" && $str [$e] <= "XBF" && $e < $limit)
+ + $e;
}
}
Return substr ($str, $s, $e-$s);
}
}
?>
http://www.bkjia.com/PHPjc/444857.html www.bkjia.com true http://www.bkjia.com/PHPjc/444857.html techarticle The following two functions are two double-byte string interception function, that is, for the Chinese string interception, good first Chinese character interception function is leapfrog concise, the latter a complex but ...