转自:http://hi.baidu.com/hiosiao/blog/item/f90e7fe7363ff128b83820b3.html
Hiosiao functioned~
function subwords($words,$num){
//从 $words 字符串中 截取前 $num 个单词
// functioned by PHP designer Hiosiao,
$total_num = str_word_count($words); // 获取$words 中单词个数
if($total_num <= $num){ // 如果$words 中单词个数少于 $num return $words; }else{ $pattern = '/([\S]+?[ ]+){'.$num.'}/'; preg_match($pattern, $words, $out); return $out[0].'...'; //如果字符串过长,将以 ... 结束 } }[/php]