0
点赞
收藏
分享

微信扫一扫

php截取字符串比较有用的。中英文都可以


function str_cut($sourcestr,$cutlength,$suffix='...')
{
$str_length = strlen($sourcestr);
if($str_length <= $cutlength) {
return $sourcestr;
}
$returnstr='';
$n = $i = $noc = 0;
while($n < $str_length) {
$t = ord($sourcestr[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$i = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$i = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t <= 239) {
$i = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$i = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$i = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$i = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if($noc >= $cutlength) {
break;
}
}
if($noc > $cutlength) {
$n -= $i;
}
$returnstr = substr($sourcestr, 0, $n);


if ( substr($sourcestr, $n, 6)){
$returnstr = $returnstr . $suffix;//超过长度时在尾处加上省略号
}
return $returnstr;
}

这个是源自于yourphp里的一段。非常好用。也非常有用

举报

相关推荐

0 条评论