Home  >  Article  >  Backend Development  >  Function to solve the problem of Chinese and English string length_PHP tutorial

Function to solve the problem of Chinese and English string length_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:57:281103browse

Copy code The code is as follows:

function strSplit($s, $len) {
$end = '...';
$result = '';
$strLen = strlen($s);
if ($strLen <= $len) {
return $s;
}
$len -= 2;
for ($i=0; $i<$len && $i<$strLen; $i++) {
$c = $s[$i];
if (ord( $c) < 0x80) {
$result .= $c;
} elseif ($i+1<$len) {
$result .= $s[$i++] . $s[ $i];
}
}
return ($i < $strLen) ? ($result . $end) : $result;
}

echo strSplit(' 1234567', 10), '
';
echo strSplit('1234567890', 10), '
'; ), '
';
echo strSplit('All are in Chinese', 10), '
';
echo strSplit('All a and b are c d中文', 10), '
';

Output:
1234567
1234567890
1234 Chinese...
All are...
All a and all b...

http://www.bkjia.com/PHPjc/317788.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317788.htmlTechArticleCopy the code The code is as follows: functionsstrSplit($s,$len){ $end='…'; $result= ''; $strLen=strlen($s); if($strLen=$len){ return$s; } $len-=2; for($i=0;$i$len$i$strLen;$i++ ){ $c=$s[$i];...
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