php: generate a 60 char random string

2019-12-29 00:00:00 IN1 , 2023-04-10 14:16:30 IN1


Q: how to create a 60 char random string ?

A: use the benath function generateRandomString() to create such a string. Modify to your needs.

usage

$srandomString = enerateRandomString();

it will produce a sting like:
1572437300-71425db97d34ae5da7-37619268-DSCbkJT0JoqHpPtsCiN1l

/**
 * @return string
 */
function generateRandomString()
{
    $iLength = 30;
    $sBase = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $iCharLength = strlen($sBase);
    $sRandom = '';

    for ($iCnt = 0; $iCnt < $iLength; $iCnt++)
    {
        $sRandom.= $sBase[rand(0, $iCharLength - 1)];
    }

    $sString = uniqid(microtime(true), true)
        . '.' . $sRandom;
    $sString = preg_replace('/[^a-zA-Z0-9]+/', '-', trim($sString));
    $sString = preg_replace('!\s+!', '-', $sString);
    $sString = substr($sString, 0, 60);

    return $sString;
}
This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".