假设我有一个如下所示的段落
This is sentence 1.This is sentence 2.This is sentence 3.This is sentence 4.This is sentence 5.This is sentence 6.
我需要从上面的段落中得到随机的3个句子。结果如下所示
This is sentence 5.This is sentence 1.This is sentence 3.
我知道它有可能爆炸。有没有简单的方法呢?
$game = explode($string, ".")
shuffle($game);
答案 0 :(得分:1)
explode
中的参数顺序错误 - 它是explode ( string $delimiter , string $string [, int $limit ] )
。你应该像这样使用它:
$game = explode('.', $string);
你会得到一个带句子的数组。现在只需打印一个:
$id = rand(0, (count($game)-1));
echo $game[$id] . '. '; //mind adding removed dot and a space after it!
如果你需要三个句子,你可以简单地循环运行它。请记住每次迭代都会生成新的$id
!