Jump to content

Generator fail.


President Obama

Recommended Posts

Had a crack at making my own generator which makes some jumble 32 characters long with letters and numbers. In logic it seemed fine to me. It practice it just does nothing.

function generate(){
$abc = 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"); 
$num = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
$result = ""; 

for ($i = 0; $i < 32; $i++){
	$bool = rand(0,1);
	if ($bool = 1) {
		$result . $abc[rand(0,25)];	
	} else {
		$result . $num[rand(0,9)];
	}
}
echo $result;
}

Link to comment
https://forums.phpfreaks.com/topic/224200-generator-fail/
Share on other sites

or a function

 

<?php
function generateString($length) {
    $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $string = "";    
    for ($s = 0; $s < $length; $s++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}

//usage
$randomstring = generateString(32);//define amount of characters
echo $randomstring;
?>

Link to comment
https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158450
Share on other sites

oops forgot to add the -1 to define

also added more numbers to offset the letters more

 

<?php
function generateString($length) {
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $string = "";    
    for ($s = 0; $s < $length; $s++) {
        $string .= $characters[mt_rand(0, strlen($characters)-1)];
    }
    return $string;
}

//usage
$randomstring = generateString(32);//define amount of characters
echo $randomstring;
?>

 

Link to comment
https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158473
Share on other sites

You can even just pull a random md5 if wanted, and do range for length 1 to 32

 

<?php
function uniqueString($length) {
$string = substr(md5(uniqid()), 0,$length);
return $string;
}
//usage
$random_string = uniqueString(10);//min 1 to max 32 for length
echo $random_string;
?>

Link to comment
https://forums.phpfreaks.com/topic/224200-generator-fail/#findComment-1158493
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.