其中评论截取开头一部分的code如下:
foreach ($comments as $comment) {
$comment_author = stripslashes($comment->comment_author);
$comment_content = strip_tags($comment->comment_content);
$comment_content = stripslashes($comment_content);
$comment_excerpt =substr($comment_content,0,42);
$comment_excerpt = utf8_trim($comment_excerpt);
$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
$output .= $before . $comment_author . ': ' . $comment_excerpt . '...' . $after;
}
最关键的就是那个utf8_trim函数了,如下
// A trim function to remove the last character of a utf-8 string
// by following instructions on http://en.wikipedia.org/wiki/UTF-8
// dotann
function utf8_trim($str) {
$len = strlen($str);
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ' '.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}
我也曾经找过一些对于中文字符处理的php函数,都不是很好,现在这个方案解决的很完美阿。