Home >Backend Development >PHP Tutorial >PHP Chinese string interception function_PHP tutorial

PHP Chinese string interception function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:08:38975browse

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 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

http: //www.bkjia.com/PHPjc/444857.htmlTechArticleThe following two functions are two double-byte string interception functions, which are for Chinese string interception , well the first Chinese character interception function is super simple, the latter one is complicated but...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn