naeemqaswar
2/28/2017 - 9:47 AM

Generate random strings and numbers

Generate random strings and numbers

public function random_string($length = 10)
{
    return strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, $length));
}

// Will generate random string, consists of only numbers
function generate_random_number($length = 5) {
    $characters = '0123456789';
    $randomValue = '';
    for ($i = 0; $i < $length; $i++) {
        $randomValue .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomValue;
}

// Will generate random string, consists of only alphabets
function generate_random_string($length = 5) {
    $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomValue = '';
    for ($i = 0; $i < $length; $i++) {
        $randomValue .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomValue;
}

// Will generate random string, consists of number and alphabets
function generate_random_mix_string($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomValue = '';
    for ($i = 0; $i < $length; $i++) {
        $randomValue .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomValue;
}