-3

I would like to know how I can get the script below to generate 300 sets of random characters.

This would avoid my having to press the reload button, 300 times.

Here is my script:

<?php

function GetID($x){     

$characters = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "2", "3", "4", "5", "6", "7", "8", "9");

shuffle($characters);

for (; strlen($ReqID)<$x;){
$ReqID .= $characters[mt_rand(0, count($characters))];
}

return $ReqID;

}     


$ReqID .= GetID(5);
       $ReqID .= "-";
$ReqID .= GetID(9);
       $ReqID .= "-";
$ReqID .= GetID(5);

echo $ReqID;

$fh = fopen("300_file.txt","a+");

fwrite($fh, ("$ReqID")."\n");

fclose($fh);

?>

Plus, if there's a way to simplify the characters as an array, would be a bonus but not required.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

2 Answers2

3

To do the 300 loop, use a for, with your bounds, 0-299 [300]:

for($l=0;$l<=299;$l++){
    $out[$l] = array_merge(range('A','Z'),range('a','z'),range(2,9));
}

The resulting array, $out, will have 0-299 as the key, and the random characters as the value.

If you want to save this to a file, do:

$content = implode("\n", $out);

And save the $content string to a file.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
  • Thank you. I've got the script generating 300 sequences, but now am trying to figure out how to have them each on a seperate line. The 300 numbers can't be in one big chunk/line. The $content=implode("\n", $out); is not working for me, for some reason. – Funk Forty Niner Jun 06 '12 at 03:39
  • If you want an HTML line breaks, do: implode("
    \n", $out); otherwise if you cat the outfile, each entry should be a separate line
    – Mike Mackintosh Jun 06 '12 at 12:00
1

simplify your array with

array_merge(range('A','Z'),range('a','z'),range(2,9))

use a loop to wash, rinse and repeat 300 times

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79