Php 从段落中随机抽取句子

Php 从段落中随机抽取句子,php,Php,假设我有下面这样一段话 This is sentence 1.This is sentence 2.This is sentence 3.This is sentence 4.This is sentence 5.This is sentence 6. This is sentence 5.This is sentence 1.This is sentence 3. 我需要从上面的段落中随机得到3个句子,结果如下 This is sentence 1.This is sentence 2.T

假设我有下面这样一段话

This is sentence 1.This is sentence 2.This is sentence 3.This is sentence 4.This is sentence 5.This is sentence 6.
This is sentence 5.This is sentence 1.This is sentence 3.
我需要从上面的段落中随机得到3个句子,结果如下

This is sentence 1.This is sentence 2.This is sentence 3.This is sentence 4.This is sentence 5.This is sentence 6.
This is sentence 5.This is sentence 1.This is sentence 3.
我知道爆炸是可能的。有什么简单的方法吗

$game = explode($string, ".")
shuffle($game);

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

哈!您的解决方案非常简单明了。请继续使用it@Max:但是如果她只需要三个元素,那么洗牌整个阵列有什么意义?