Php 改变随机句子的顺序

Php 改变随机句子的顺序,php,Php,我有一篇长文本(3600个句子),我想改变随机句子的顺序。有一些简单的PHP脚本可以改变句子的顺序?您可以这样完成。在句子的结尾爆开一条线,例如句号。使用Shuffle函数洗牌数组。然后内爆字符串,将句号加回来 输出类似于: 你好,这是一句话。这是第五个。这是第四次。这是第二次。。这是第三个 $sentences = 'Hello, this is one sentence. This is a second. THis is a third. This is a forth. This is

我有一篇长文本(3600个句子),我想改变随机句子的顺序。有一些简单的PHP脚本可以改变句子的顺序?

您可以这样完成。在句子的结尾爆开一条线,例如句号。使用
Shuffle
函数洗牌数组。然后内爆字符串,将句号加回来

输出类似于:

你好,这是一句话。这是第五个。这是第四次。这是第二次。。这是第三个

$sentences = 'Hello, this is one sentence. This is a second. THis is a third. This is a forth. This is a fifth.';

$sentencesArray = explode('.', $sentences);
array_filter($sentencesArray);
shuffle($sentencesArray);

$sentences = implode('.', $sentencesArray);


var_dump($sentences);

我构建了一个解决方案,解决了以“.”、“!”或“?”结尾的句子的问题。我注意到将句子数组的最后一部分包含在洗牌中不是一个好主意,因为最后一部分永远不会以我们正在拆分的特定字符结束:

“嗨。|你好。|”

我希望你能明白。所以我洗牌所有的元素,除了最后一个。我分别为“.”、“?”和“!”做这项工作

你应该知道“…”、“?!”、“!!!11!!1!!”会引起大麻烦。:)



将句子放在一个数组中,然后使用
shuffle
将数组随机排列。请展示您尝试过的内容,我们不是来为您工作的。您的所有句子都以“.”结尾,还是也可以以“?”或“!”结尾?我刚刚意识到“…”和“?!”在语法上也是结束句子的正确方法。。。。让我们不要再谈论“!!!11!!1!!”而这些…它是有效的!你是个天才,我爱你!:)有可能实现“?”和“!”吗?我不是程序员。只需在此处插入符号-(“.”?“!”,$句)?非常感谢。
<?php
function randomizeOrderOnDelimiter($glue,$sentences){

    $sentencesArray = explode($glue, $sentences);

    // Get out the items to shuffle: all but the last.
    $work = array();
    for ($i = 0; $i < count($sentencesArray)-1; $i++) {
        $work[$i] = $sentencesArray[$i];
    }

    shuffle($work);  // shuffle them

    // And put them back.
    for ($i = 0; $i < count($sentencesArray)-1; $i++) {
        $sentencesArray[$i] = $work[$i];
    }

    $sentences = implode($glue, $sentencesArray);
    return $sentences;
}

$sentences = 'Hello, this is one sentence. This is a second. THis is a third. This is a forth. This is a fifth. Sixth is imperative! Is seventh a question? Eighth is imperative! Is ninth also a question? Tenth.';
$sentences = randomizeOrderOnDelimiter('.', $sentences);
$sentences = randomizeOrderOnDelimiter('?', $sentences);
$sentences = randomizeOrderOnDelimiter('!', $sentences);
var_dump($sentences);

?>