Take the 2-minute tour ×

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 ?


    $array = explode(',', $translated);
    $randomKeys = array_rand($array, 3);
    $translated = '';
    foreach(array_keys($randomKeys) as $key){
      $translated .= $array[$key].' ';  // use space or comma

    echo $translated;

echos: water kip banaan always. so it does not seem to shuffle well?

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

you can use

$randomKeys = array_rand($array, 3);
$str = '';
foreach($randomKeys as $key){
  $str .= $array[$key].' ';  // use space or comma
share|improve this answer
$randomString = implode(',' array_rand($array, 3)); this line has a error it seems or is dreamweaver a silly app? –  Sam Mar 1 '11 at 14:33
@Sam : It was a mistake of , , I changed it. –  Gaurav Mar 1 '11 at 15:40
@Gaurav, thanks! +1 and now how do I echo that? –  Sam Mar 1 '11 at 15:55
echo $randomString; –  Gaurav Mar 1 '11 at 16:00
tried that but see 4,5,7 on the screen –  Sam Mar 1 '11 at 16:01
show 6 more comments
function randomstr($str, $num = 3) {
   $str = explode(',', $str);
   return implode(',' array_rand($array, $num));


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

Or in function __:

function __($string) {
   $string = randomstr($string);
   // more your code
share|improve this answer
__($...) is not allowed as written in the question. Must be words inbetween sorry I wasn't clear (updated question) –  Sam Mar 1 '11 at 14:36
@Sam did you tried second variant (function __($string) {)? –  delphist Mar 1 '11 at 14:51
yep, that also doesnt work on my site. relly can only touch up the @translatedString only, not the __("..."); part. –  Sam Mar 1 '11 at 15:38
add comment

Would it be possible to store the values not as string, but as an array? If you cant just explode them by , then get a random element from the array and translate that one.

For example:

$translated = __("water, soup, rice, peanutbutter");
$translatedWords = explode(',',$translated);
shuffle($translatedWords); //Randomize them

Would output something like Soep, Rijst, Pindakaas, Water

(Also __ is quite an odd name for a function)

share|improve this answer
Actually __ is not a weird name. _ is used by gettext, and since this function does a similar job (translating stuff), I'd say the name is adequate. –  Artefact2 Mar 1 '11 at 13:52
Indeed, and its very short = love it! –  Sam Mar 1 '11 at 14:30
I like your solution +1, but the question was to get only ,say, 3 items not the entire string shuffled. How would you implement the selective grasp in your proposal? –  Sam Mar 1 '11 at 14:35
echo shuffle($translatedWords); echoes 1, that means you are using arrays right? so how to echo this array correctly? –  Sam Mar 1 '11 at 15:22
You can view the contents of the entire array by using var_dump, you can just echo the first 3 elements of the array using a simple for loop. @Artefact2: I believe __ doesnt really describe what the function does. –  TJHeuvel Mar 2 '11 at 9:38
add comment

Your Answer


