How I can get random words without repeating in PHP? Help
+4
A:
$words = preg_split('//', 'abcdefghijklmnopqrstuvwxyz0123456789', -1);
shuffle($words);
foreach($words as $word) {
echo $word . '<br />';
}
karim79
2010-07-25 16:06:48
Is no repeating?
ABC Girl
2010-07-25 16:08:42
@ABC Girl - That will shuffle the array of words, and print them out one by one, so no, no repeating.
karim79
2010-07-25 16:09:21
If the array can have double values add `$words = array_unique($words);` before the `shuffle()`
Wrikken
2010-07-25 16:11:25
@karim79 Thanks, but I need random words with 0 to 9 and a to z keywords for my ID table. So, I don't need repeat.
ABC Girl
2010-07-25 16:15:03
@ABC Girl - something like that? I'm having trouble understanding what you mean...
karim79
2010-07-25 16:20:45
+2
A:
Use this list of word put it into a text file. Then use the file() function to read the words into an array and select two random elements using array_rand()
Shubham
2010-07-25 16:11:20
A:
function getrandomstring($length) {
global $template;
settype($template, "string");
$template = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* this line can include numbers or not */
settype($length, "integer");
settype($rndstring, "string");
settype($a, "integer");
settype($b, "integer");
for ($a = 0; $a <= $length; $a++) {
$b = rand(0, strlen($template) - 1);
$rndstring .= $template[$b];
}
return $rndstring;
}
conlippert
2010-07-25 16:11:25
Is without repeat? I mean, I have use this random keywords in database table ID. So, I don't need repeat keywords. Thanks
ABC Girl
2010-07-25 16:20:34