解决中英文字符串长度问题函数
代码如下:
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), '<br />';
echo strSplit('1234567890', 10), '<br />';
echo strSplit('1234中文567890abcdefghijkl', 10), '<br />';
echo strSplit('全部都是中文', 10), '<br />';
echo strSplit('全a部b都c是d中e文', 10), '<br />';
输出:
1234567
1234567890
1234中文…
全部都是…
全a部b都… 相关推荐
KilluaZoldyck 2020-01-29
MySQLl 2019-04-04
lightindark 2020-06-03
baishuwei 2020-06-03
zmjzhangmj 2020-04-20
rongxionga 2020-01-20
playlinuxxx 2010-02-22
过儿古墓 2011-01-12
Crazyshark 2019-06-25
amberom 2015-07-01
zhangkala 2013-06-08
技术渣 2016-11-07
cxymds 2019-06-27
静心斋 2013-08-23
dabian 2012-07-06
gmmargin 2012-12-26
张金乙 2012-08-07
WasteLand 2017-09-22
小小时光 2018-04-13