Answers for "php random character generator"

PHP
28

PHP random string generator

function generateRandomString($length = 25) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
//usage 
$myRandomString = generateRandomString(5);
Posted by: Guest on October-30-2019
3

randomstring php

//generates 13 character random unique alphanumeric id
echo uniqid();
//output - 5e6d873a4f597
Posted by: Guest on June-08-2020
1

generate random string in php

/**
 * Generate a random string, using a cryptographically secure 
 * pseudorandom number generator (random_int)
 *
 * This function uses type hints now (PHP 7+ only), but it was originally
 * written for PHP 5 as well.
 * 
 * For PHP 7, random_int is a PHP core function
 * For PHP 5.x, depends on https://github.com/paragonie/random_compat
 * 
 * @param int $length      How many characters do we want?
 * @param string $keyspace A string of all possible characters
 *                         to select from
 * @return string
 */
function random_str(
    int $length = 64,
    string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
): string {
    if ($length < 1) {
        throw new \RangeException("Length must be a positive integer");
    }
    $pieces = [];
    $max = mb_strlen($keyspace, '8bit') - 1;
    for ($i = 0; $i < $length; ++$i) {
        $pieces []= $keyspace[random_int(0, $max)];
    }
    return implode('', $pieces);
}
Posted by: Guest on April-19-2021
0

Generate Random String in PHP

phpCopy<?php 
$Random_str = uniqid();  
echo "Random String:", $Random_str, "\n";
?>
Posted by: Guest on April-23-2021
0

Generate Random String in PHP

phpCopy<?php
 
echo "Out1: ",substr(md5(time()), 0, 16),"\n";
 
echo "Out2: ",substr(sha1(time()), 0, 16),"\n";
 
echo "Out3: ",md5(time()),"\n";
 
echo "Out4: ",sha1(time()),"\n";
 
?>
Posted by: Guest on April-23-2021
0

Generate Random String in PHP

phpCopy<?php
 
function secure_random_string($length) {
    $rand_string = '';
    for($i = 0; $i < $length; $i++) {
        $number = random_int(0, 36);
        $character = base_convert($number, 10, 36);
        $rand_string .= $character;
    }
 
    return $rand_string;
}
 
echo "Sec_Out_1: ",secure_random_string(10),"\n";
 
echo  "Sec_Out_2: ",secure_random_string(10),"\n";
 
echo  "Sec_Out_3: ",secure_random_string(10),"\n";
 
?>
Posted by: Guest on April-23-2021

Code answers related to "php random character generator"

Browse Popular Code Answers by Language