分类: LINUX
2005-08-05 11:25:54
function trimBody($theText, $lmt=500, $s_chr=" ", $s_cnt=2)
一个实现字符串 截断的函数。
function trimBody($theText, $lmt=500, $s_chr="
", $s_cnt=2) {
$pos = 0;
$trimmed = FALSE;
for ($i = 1; $i <= $s_cnt; $i++) {
if ($tmp = strpos($theText, $s_chr, $pos+1)) {
$pos = $tmp;
$trimmed = TRUE;
} else {
$pos = strlen($theText) - 1;
$trimmed = FALSE;
break;
}
}
$theText = substr($theText, 0, $pos);
if (strlen($theText) > $lmt) {
$theText = substr($theText, 0, $lmt);
$theText = substr($theText, 0, strrpos($theText," "));
$trimmed = TRUE;
}
if ($trimmed) $theText .= "...";
return $theText;
}