tags:

views:

155

answers:

3

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 />';
}

http://php.net/manual/en/function.shuffle.php

karim79
Is no repeating?
ABC Girl
@ABC Girl - That will shuffle the array of words, and print them out one by one, so no, no repeating.
karim79
If the array can have double values add `$words = array_unique($words);` before the `shuffle()`
Wrikken
@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
@ABC Girl - something like that? I'm having trouble understanding what you mean...
karim79
+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
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
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