PHP Random String Generator

This script will generate a random string… (ideal for URL shorteners, Pastebins and confirm URLs.)

<?php
/*
Niall Davies' Random String Generator
This program makes a random string containing hexidecimal characters.
Ideal for URL shorteners, Pastebins, etc.
Originally scripted for Pasteit.co.uk
*/
Function RandStr()
{
/* You will need to configure this part... */
$min_number_of_chars = 3;
$max_number_of_chars = 5;
/* No configuring now... */
$number_of_chars = rand($min_number_of_chars, $max_number_of_chars);
$string = '';
for ($x=1; $x <= $number_of_chars; $x++) {
$new = RandChar();
$string .= $new;
}
return $string;
}
Function RandChar()
{
$upper = Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$lower = Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$which = rand(0,2);
switch ($which) {
case 0:    return $upper[rand(0,count($upper)-1)];
break;
case 1: return $lower[rand(0,count($lower)-1)];
break;
case 2: return rand(0,9);
break;
}
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *