Jump to content

random string


karimali831

Recommended Posts

Hi,

 

I'm having trouble generating random string with 8 characters, when I refresh I sometimes get

6 characters and sometimes 8. "-" not included as character.

 

Wrong:

6JW-F7W

 

Correct: (should always generate random like this)

DVMF-F36V

 

I know I'm trying to achieve this in wrong methods but it's the only way I know.

Hope someone can help.

 

function getReference() {
    $length = 8;
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz"; 

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    
    return strtoupper(substr($string, 0, -$length/2)."-".substr($string, $length/2-1, -1));
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/233631-random-string/
Share on other sites

<?php
function getReference($size = 4) {
$size = $size > 36 ? 30 : $size;
$pool = array_merge(range(0, 9), range('a', 'z'));
$rand_keys = array_rand($pool, $size);

	$random = '';

foreach ($rand_keys as $key) {
	$random .= $pool[$key];
}

return $random ;
}

echo getReference(4).'-'.getReference(4);
?>

 

Something like this?

Link to comment
https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201230
Share on other sites

You can also use the shuffle and array_slice functions:

<?php
function getReference($size = 4) {
$pool = array_merge(range(0, 9), range('a', 'z'));
//
// if you want to include uppercase letters, change the above line to
// $pool = array_merge(range(0,9),range('a','z'),range('A','Z'));
$size = $size > count($pool) ? 30 : $size; // base it on the size of the array of possible values, so if you add more this doesn't have to change
        shuffle($pool);
        return (implode('',array_slice($pool,0,$size)));
}

echo getReference(4).'-'.getReference(4);
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201246
Share on other sites

The problem with using MD5 is that it returns a hexadecimal number (characters 0 - F) and not the full alphabet like the OP was using.

 

BTW, the code you posted is missing two ")"...

 

It could have been written as

<?php
function gr($size=4) {
return (strtoupper(substr(md5(rand()*rand()/rand()), rand(1, 20), $size) . '-' . substr(md5(rand()*rand()/rand()), rand(1, 20),$size)));
}

echo gr();
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201290
Share on other sites

The problem with using MD5 is that it returns a hexadecimal number (characters 0 - F) and not the full alphabet like the OP was using.

 

BTW, the code you posted is missing two ")"...

 

It could have been written as

<?php
function gr($size=4) {
return (strtoupper(substr(md5(rand()*rand()/rand()), rand(1, 20), $size) . '-' . substr(md5(rand()*rand()/rand()), rand(1, 20),$size)));
}

echo gr();
?>

 

Ken

 

Haha, Thanks, I just realised. It was intended to be an example though but yeah I suppose.

Link to comment
https://forums.phpfreaks.com/topic/233631-random-string/#findComment-1201292
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.