Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the order of random sentences

Tags:

php

I have a long text (3600 sentences) and I want to change the order of random sentences. There are some simple PHP script that can change the order of sentences?

like image 448
JiriJur Avatar asked Nov 21 '25 12:11

JiriJur


1 Answers

You can accomplish it like this. Explode a string on the end of a sentence e.g full stop. Shuffle the array using the shuffle function. Then implode the string, adding the full stops back.

The output will be something like:

Hello, this is one sentence. This is a fifth. This is a forth. This is a second.. THis is a third

$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);
like image 152
CharliePrynn Avatar answered Nov 24 '25 03:11

CharliePrynn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!