Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

php random string generator with custom choise

Im trying to make a "special" string generator, with a custom selection for wich characters you want.
shortly in the code when you call this function:

generateRandomString(length, [special characters], [numbers], [lower characters], [upper characters]);

for example:
generateRandomString(5, true, true, true, true);

the code should be max 5 characters, with letters, numbers and special charaters… like: fE3%!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

but is give me 5 random string for each bool active so if it 4 i have back 20 characters instead of 5

this is the code
what im doing wrong?

function generateRandomString($length, $special, $numbers, $upper, $lower)
{
    //$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $characters["special"] = "!%";
    $characters["numbers"] = "01";
    $characters["upper"] = "ABC";
    $characters["lower"] = "abc";
    $randomString = '';
 
    for ($i = 0; $i < $length; $i++)
    {
        if($special)
        {
            $randomString .= $characters["special"][rand(0, strlen($characters["special"]) - 1)];
        }
        if($numbers)
        {
            $randomString .= $characters["numbers"][rand(0, strlen($characters["numbers"]) - 1)];
        }
        if($upper)
        {
            $randomString .= $characters["upper"][rand(0, strlen($characters["upper"]) - 1)];
        }
        if($lower)
        {
            $randomString .= $characters["lower"][rand(0, strlen($characters["lower"]) - 1)];
        }
    }
 
    return $randomString;
}

>Solution :

You should first build valid characters range based on given parameters, and only then build random string.

Added validator to ensure that at least one character from each required group exists in random string.

function generateRandomString($length, $special, $numbers, $upper, $lower)
{
    //$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $characters["special"] = "!%";
    $characters["numbers"] = implode('', range(0, 9));
    $characters["upper"] = implode('', range('A', 'Z'));
    $characters["lower"] = implode('', range('a', 'z'));

    $charactersSet = '';
    $validators = [];
    $randomString = '';

    if ($special) {
        $charactersSet .= $characters["special"];
        $validators[] = '/[' . preg_quote($characters["special"]) . ']/';
    }

    if ($numbers) {
        $charactersSet .= $characters["numbers"];
        $validators[] = '/\d/';
    }
    
    if ($upper) {
        $charactersSet .= $characters["upper"];
        $validators[] = '/[A-Z]/';
    }

    if ($lower) {
        $charactersSet .= $characters["lower"];
        $validators[] = '/[a-z]/';
    }

    for ($i = 0; $i < $length; $i++) {
        $randomString .= $charactersSet[rand(0, strlen($charactersSet) - 1)];
    }

    foreach ($validators as $pattern) {
        if (preg_match($pattern, $randomString) === 0) {
            $randomString = generateRandomString($length, $special, $numbers, $upper, $lower);

            break;
        }
    }

    return $randomString;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading