PHP中字符串截断函数

[入库:2005年9月19日] [更新:2007年3月24日]

本文简介:

function trimBody($theText, $lmt=500, $s_chr="\n", $s_cnt=2)

一个实现字符串 截断的函数。

function trimBody($theText, $lmt=500, $s_chr="\n", $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;
}

本文关键:PHP中字符串截断函数
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top