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 朋友坐车走了祝福语简短
18 给英雄的祝福语简短