随机选择php字符串

时间:2017-11-18 19:50:37

标签: php

我有一个看起来像这样的字符串

$genres = "pop,rock,jazz,80s";

我想知道是否有可能创建一个随机选择上述任何类型的字符串?但删除逗号?

例如,它会创建

$newgenre = "pop";

4 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

var duration;
audio.onloadedmetadata = function(){
    duration = audio.duration;
    setTimeout(function(){
        callback(play);
    }, duration);
}

audio.play();

答案 1 :(得分:1)

替代方法

其他人正在使用数组中的随机选择,请尝试以下方法:

$genreArray = explode(',', $genres);
shuffle($genreArray);     //mixes up the array using rand or mt_rand 
$genre = $genreArray[0];  // take first element of shuffled array.

答案 2 :(得分:0)

您可以使用$genreArray = explode(',', $genres)函数将其全部放入数组中。 然后,您可以使用

为数组生成随机索引
$randomKey = rand(0, count($genreArray))

然后,你所要做的就是从数组中获取随机类型。

$randomGenre = $genreArray[$randomKey];

答案 3 :(得分:0)

你可以爆炸

从数组中获取一个随机值

$genresArray = explode(',',$genres);

$your_values = array_rand ($genresArray,1);

echo $your_values[0];