1 Vote Vote

How to Shuffle and Echo 3 Random Words out of a String?

Posted by topdog 448 days ago Questions| words random random words All

Hi folks, a question about getting some random words out of a bigger string after it has been translated:

<?=__("water, chicken, banana, rice, bread, salt, cucumber, ananas, peach")?>

on my site currently outputs:

water, kip, banaan, rijst, zout, komkommer, ananas, perzik

now imagine I want to get just 3 words from that on random. How do I do that?

It's important not to touch the words parts inside __(" & ") part! The translater cannot process when __($var) but ONLY when __("word1, word2, word3").

I imagine the best is to first put the the outcome into a string or array (this is how far I came please don't laugh)

$translated = __("water, chicken, banana, rice, bread, salt, cucumber");
echo $translated;
# shuffle & echo 3 items

How do I proceed from here to randomly get 3 words out of $entireString ?

Originally asked by: Sam on Stack Overflow

Discuss Bury


Who Voted for this Question