php怎么按字数截取替换
php怎么按字数截取替换?
PHP截取指定长度的字符串,超出部分用 ..替换
function substr_format($text, $length, $replace='..', $encoding='UTF-8')
{
if ($text && mb_strlen($text, $encoding)>$length)
{
return mb_substr($text, 0, $length, $encoding).$replace;
}
return $text;
}
还有
function cut_string($str, $len)
{
// 检查长度
if (mb_strwidth($str, 'UTF-8')<=$len)
{
return $str;
}
// 截取
$i = 0;
$tlen = 0;
$tstr = '';
while ($tlen < $len)
{
$chr = mb_substr($str, $i, 1, 'UTF-8');
$chrLen = ord($chr) > 127 ? 2 : 1;
if ($tlen + $chrLen > $len) break;
$tstr .= $chr;
$tlen += $chrLen;
$i ++;
}
if ($tstr != $str)
{
$tstr .= '...';
}
return $tstr;
}
以上就是php怎么按字数截取替换的详细内容,更多请关注毛票票其它相关文章!
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短