讨论数量:
public static function text($maxNbChars = 200)
{
if ($maxNbChars < 5) {
throw new \InvalidArgumentException('text() can only generate text of at least 5 characters');
}
$type = ($maxNbChars < 25) ? 'word' : (($maxNbChars < 100) ? 'sentence' : 'paragraph');
$text = array();
while (empty($text)) {
$size = 0;
// until $maxNbChars is reached
while ($size < $maxNbChars) {
$word = ($size ? ' ' : '') . static::$type();
$text[] = $word;
$size += strlen($word);
}
array_pop($text);
}
if ($type === 'word') {
// capitalize first letter
$text[0] = ucwords($text[0]);
// end sentence with full stop
$text[count($text) - 1] .= '.';
}
return implode('', $text);
}
你可以指定需要生成文本的长度,但必须大于5
这是text方法的源码 读一下就明白了
推荐文章: