$body_len = 135;
if(strpos($blog['Body'],"<img") !== false){
$startStr = strstr($blog['Body'],"<img");
$endPos = strpos($startStr,"/>");
$imgStr = substr($startStr,0,$endPos+2);
}//把第一张照片给提取出来
if(!$imgStr){
$body_len += 56;
}
$summary = strip_tags($blog['Body']);//去掉所有的标签
$summary = str_replace(' ','',$summary); //去掉
$summary = str_replace(' ','',$summary);//去掉所有空格
$summary = preg_replace('//r|/n/', '', $summary); //去掉换行
if(strlen($summary) > $body_len){
$summary = ms_utf8_substr($summary,$body_len);//截取,如果直接用substr的话,因为中文字符的问题,会产生乱码。
$summary .= "...";
}
function ms_utf8_substr($string,$length,$encode = 0) {//核心方法
// 增加对html编码的 先解码,再截断,再编码的支持。
if ($encode){
$string = htmlspecialchars_decode($string);
}
$words = 0;
$n = 0;
while ($n < strlen($string) && $words < $length ) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 127)) {
$n++;
$words ++;
} elseif(192 <= $t && $t <= 223) {
$n += 2;
$words += 2;
} elseif(224 <= $t && $t <= 239) {
$n += 3;
$words += 2;
} elseif(240 <= $t && $t <= 247) {
$n += 4;
$words += 2;
} elseif(248 <= $t && $t <= 251) {
$n += 5;
$words += 2;
} elseif($t == 252 || $t == 253) {
$n += 6;
$words += 2;
} else {
$n++;
$words ++;
}
}
$wordscut = substr($string, 0, $n);
if ($encode){
$wordscut = htmlspecialchars($wordscut);
}
return $wordscut;
}