generate random string in php

Solutions on MaxInterview for generate random string in php by the best coders in the world

showing results for - "generate random string in php"
Juan Esteban
16 Nov 2019
1/**
2 * Generate a random string, using a cryptographically secure 
3 * pseudorandom number generator (random_int)
4 *
5 * This function uses type hints now (PHP 7+ only), but it was originally
6 * written for PHP 5 as well.
7 * 
8 * For PHP 7, random_int is a PHP core function
9 * For PHP 5.x, depends on https://github.com/paragonie/random_compat
10 * 
11 * @param int $length      How many characters do we want?
12 * @param string $keyspace A string of all possible characters
13 *                         to select from
14 * @return string
15 */
16function random_str(
17    int $length = 64,
18    string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
19): string {
20    if ($length < 1) {
21        throw new \RangeException("Length must be a positive integer");
22    }
23    $pieces = [];
24    $max = mb_strlen($keyspace, '8bit') - 1;
25    for ($i = 0; $i < $length; ++$i) {
26        $pieces []= $keyspace[random_int(0, $max)];
27    }
28    return implode('', $pieces);
29}
30
Thiago
14 May 2017
1phpCopy<?php 
2$Random_str = uniqid();  
3echo "Random String:", $Random_str, "\n";
4?> 
5
Alois
18 Nov 2019
1phpCopy<?php
2 
3echo "Out1: ",substr(md5(time()), 0, 16),"\n";
4 
5echo "Out2: ",substr(sha1(time()), 0, 16),"\n";
6 
7echo "Out3: ",md5(time()),"\n";
8 
9echo "Out4: ",sha1(time()),"\n";
10 
11?>
12
Ilaria
31 Apr 2020
1phpCopy<?php
2 $x = 0;
3 $y = 10;
4$Strings = '0123456789abcdefghijklmnopqrstuvwxyz';
5echo "Gen_rand_str: ",substr(str_shuffle($Strings), $x, $y), "\n";
6
7$a = 0;
8$b = 20;
9$Strings='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
10echo "Gen_My_Pass: ",'hello.'.substr(str_shuffle($Strings), $a, $b).'.World',"\n";
11?>
12
Gaia
18 Jan 2017
1phpCopy<?php
2 
3function secure_random_string($length) {
4    $rand_string = '';
5    for($i = 0; $i < $length; $i++) {
6        $number = random_int(0, 36);
7        $character = base_convert($number, 10, 36);
8        $rand_string .= $character;
9    }
10 
11    return $rand_string;
12}
13 
14echo "Sec_Out_1: ",secure_random_string(10),"\n";
15 
16echo  "Sec_Out_2: ",secure_random_string(10),"\n";
17 
18echo  "Sec_Out_3: ",secure_random_string(10),"\n";
19 
20?>
21
Cyril
08 Sep 2017
1phpCopy<?php 
2echo uniqid('user_');
3?> 
4
Jessica
21 Jul 2020
1phpCopy<?php
2 
3echo "Output-1: ",bin2hex(random_bytes(10)),"\n";
4 
5echo "Output-2: ",bin2hex(random_bytes(20)),"\n";
6 
7echo "Output-3: ",bin2hex(random_bytes(24)),"\n";
8 
9?>
10
Marlene
01 Jul 2017
1phpCopy<?php
2function random_str_generator ($len_of_gen_str){
3    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
4    $var_size = strlen($chars);
5    echo "Random string ="; 
6    for( $x = 0; $x < $len_of_gen_str; $x++ ) {  
7        $random_str= $chars[ rand( 0, $var_size - 1 ) ];  
8        echo $random_str;  
9    }
10echo "\n";
11}
12random_str_generator (8)
13?>
14
queries leading to this page
php random string functionphp create human random stringrandom string in phpgenerate random string phpgenerata random number phpphp rand stringrandom generate string phpgenerate random letters and numbers phpphp string randomphp generate random string from regexrandom character phpget random string phprandom generator in phphow to generate random characters in phpphp randomg stringrandom string generator php functiongeneraterandomstring phpstr random phpphp random string generator textboxphp randomstringphp random string intrandomcode in phpphp simple random stringphp function for random stringphp random number and stringgenerate 6 character random string phpphp randomate random string characterphp generate random string 27 php random caractersstr random in phpgenerate random text phpphp generate random string of charactersgeneraterandomstring in phpgenereate randoom string in phpmake random string and number phpphp generate random string only lettersgenerating random string in phphow to generate random string in phpgenerate random words phpphp random text phpphp rand with lettersphp random stringrand string phprandom string phpstr random string phprandom snumber phpphp create random string of charactershow to generate random code in phpget a random string phpgenerate random text and character in phprandom character generator in phpphp random text generatephp random function stringget random character from string phphow to pick random character in phpchartres random string phprandom number and character generator in phpphp random string generatorhow to generate a random number of letters in php randomlyphp generate random string algenerate a random number phpquery string random in phphow to generate a random text in phpgenerating simple random characters using phpgenerate a starting string followed by a random number in phpphp random string generationhow to generate random string in php by random methodgenerate random sctring in phpis there any function in php to genrate random stringphp generate random string from charactersphp function to generate random stringphp gen random stringrandom code generator using phpphp random string and numberadd text with random number in phpgenerate random number and string phpphp random string 4 numbersrandom characters string phphow to create random text phpphp random letters and numbersstr random phprandom text and number generator phpcreate random string in phpauto generate random text in phpgenerate random string in phpphp generate random string quicklymake a random string php php random letterhow to generate a random number in phprand php stringgenerate random characters in phphow to select random string in phpphp random wordgenerating random text phpphp use random charphp algorithm to generate random strings and numbersgenerate random number in phprandom word in phphow to make a random number generator phpgenerating random strings in phpgenerate random number to include text charater in phpphp random string from listphp random 6 character stringphp generate random string from another stringphpp random stringphp create random string tutorialstr random in phpphp best function to generate random stringphp random strigphp random from stringrandom string generator in phpphp random strgenerate a random code in phpgenerate random string based on another string phphow to generate random numbers and letters in phprandom string phprandom string generator php onlinerandom number string with text in phpgenerate random pattern string in phpgenerate random with phprandom string function in php random string phpphp random string passwordget random string in phpphp generate random string from stringgenerate random text and numbers phpphp generate string of random characters4 random number generator in phphow to generate random string id in phpphp get random stringgenerate a random number in phprandom text phphow to output random string of letters in phpphp random characterrandom string generator phpphp str randomget random char in phpphp generate random string fromphp random text generatorphp generate random string regexphp random word generatorrandom letter generator in phpphp create random stringprint random number in phprandom php stringphp generate random string 10php random generatorrandom text and number phpget randome string from a string in phpphp generate random characterhow do i make a random string in phpphp sring randomgenerate 4 digit random string in php10 digit random string generator in phprandom str phprandom word and number to string in phpphp generate beautiful random stringphp random string idphp random string 10php random stringscreate random string phphow to make a random number generator in phpgenerate randon string phpphp generate random stringgenerate a random string phpphp create random wordsphp get random letterhow to generate random character and numbers in phphow give random characters in phprandom character generator phpcreate random php stringrandom letters phprandom text generator in phpgenerate random string 28 29 phpphp random character generatorgenrate ramdom string in phpphp generate random string phpcreate random text phprandom letter phpmost random string phprandom text phphow to generate a 148 character random string and number in phphow to generate random string using phpgenerate random letters phpcreate random string via phphow to get random letter in phpphp create random string with regexrandom symbols from string phpgenerate randon text phprandom string generator in phpgenerate random string from string phprandom php strrandom string number phpstring random phpgenerate random staring in phpgenerate random characters phpgenerator a random code for given number phprandom strings in phpphp random string with lengthphp get random characterphp generate random sentencephp create randomg string namgenerate random sring phpgenerate random string in php