$maxLength < 0
(为负数),终止循环就行了,否则 $strlen > $maxLength
始终成立,陷入了死循环。
public function tixingtext($string, $maxLength = 18, $start=0) {
// 获取字符串长度
$strlen = mb_strlen($string,'utf8');
$array = array();
$html = '';
while ($strlen > $maxLength) {
//循环,根据字符串长度截取赋值给数组
$array[] = mb_substr($string, $start, $maxLength, "utf8");
//截取字符串后,重新赋值
$string = mb_substr($string, $maxLength, $strlen, "utf8");
//获取剩余的字符串长度,给while条件调用
$strlen = mb_strlen($string,'utf8');
//截取长度,每循环一次减2,给while条件调用
if($maxLength < $strlen)$maxLength = $maxLength -2;
if ($maxLength < 0) break;
}
foreach($array as $k => $v){
$html .= '<div>'.$v.'</div>';
}
return $html;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…