如何从字符串中随机播放和回显3个随机单词? [英] How to Shuffle and Echo 3 Random Words out of a String?

查看:126
本文介绍了如何从字符串中随机播放和回显3个随机单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关从更大的字符串之后中提取一些随机单词的问题,该问题已被翻译:

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

现在想象一下,我想从中随机得到3个单词.我该怎么办?

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

重要的是,请勿触摸__("& ")部分! __($var)时,翻译器无法处理,而__("word1, word2, word3")时,仅翻译器.

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

我如何从这里开始从$entireString中随机抽取3个单词?

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始终.所以它似乎不能很好地洗牌?

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

推荐答案

您可以使用

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

这篇关于如何从字符串中随机播放和回显3个随机单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆