I wanted to make a very fast function to create a random paragraph .
So I thought , the best way would be to take an existing one , explode, shuffle, and implode .
$content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation .';
$content = explode(" ", $content);
shuffle ($content);
$content = implode(" ", $content);
It works, but It produced 2 questions in my mind .
1 – why is this not working : ?
$content = explode(" ", $content);
$content = shuffle ($content);
$content = implode(" ", $content);
and this does work :
$content = explode(" ", $content);
shuffle ($content);
$content = implode(" ", $content);
2 – how to randomly define a number of words to drop from the array
(in order to create a random length / number of words ? )
** UPDATE I **
First of all , thanks to all the comments answers !
I ended up with this :
$content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
';
$rand_y = rand(1, 5);// rand number for the loop
for ($b=1; $b<=$rand_y; $b++) { // repeat random times (to create paragraphes)
$content = explode(' ', $content);
$count = count($content) ; // word count - otherwise can return error
$rand_x = rand(40,$count); // min 20 words , max = word count in paragraph
$rnd = array_rand($content, $rand_x); // answer from @bsdnoobz
$content = implode(' ', array_intersect_key($content, array_flip($rnd)));
}
1- I added the count() because without it i could get an error if the rand number is bigger than the word count.
“Second argument has to be between 1 and the number of elements in the
array”
(actually it is strange that PHP does not automtically ignore such an error – but nevermind .)
2- I did not find a simple way to add a paragraph separator (carriege return or new line) to create more than one . so i used a simple “for” loop. it turns out that adding the carriege return inside the $content$ itself will work ! (I would like to know why , i think it is being counted as part of the array! )
Anyhow – I know it is a primitive way, and not what many php gurus would choose ,- but I learned a lot , and that is the most important thing – thanks to all !
shuffle()will randomize the array argument, but it returns a boolean. You can usearray_rand()to pick one or more random entries from an array.