php中文utf8字符截取--cmstop代码

1 function str_cutword($string,$length=80,$charset="utf-8",$etc='...')
2 {
3 $start = 0;
4 if (function_exists ("mb_substr" ))
5 return mb_substr ( $string, $start, $length, $charset );
6 $re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
7 $re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
8 $re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
9 $re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
10 preg_match_all ( $re [$charset], $string, $match );
11 $slice = join ( "", array_slice ( $match [0], $start, $length ) );
12
13 return $slice.$etc;
14 }

 

posted @ 2010-11-04 14:56  wingle  阅读(155)  评论(0编辑  收藏  举报