选择语言 :

 Core_Text::limit_words

Limits a phrase to a given number of words.

$text = Text::limit_words($text);

string Core_Text::limit_words( string $str [, integer $limit = integer 100 , string $end_char = null ] )

参数列表

参数 类型 描述 默认值
$str string Phrase to limit words of
$limit integer Number of words to limit to integer 100
$end_char string End character or entity null
返回值
  • string
File: ./core/classes/text.class.php
public static function limit_words($str, $limit = 100, $end_char = NULL)
{
    $limit = (int)$limit;
    $end_char = (null===$end_char) ? '…' : $end_char;

    if (trim($str) === '')return $str;

    if ($limit <= 0)return $end_char;

    preg_match('/^\s*+(?:\S++\s*+){1,' . $limit . '}/u', $str, $matches);

    // Only attach the end character if the matched string is shorter
    // than the starting string.
    return rtrim($matches[0]) . ((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
}