How to generate a random, unique, alphanumeric string in PHP
There are many ways to generate a random, unique, alphanumeric string in PHP which are given below:
- Using str_shuffle() Function: The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter. When a number is passed, it treats the number as the string and shuffles it. This function does not make any change in the original string or the number passed to it as a parameter. Instead, it returns a new string which is one of the possible permutations of the string passed to it in the parameter.
Example:
<?php// This function will return a random// string of specified lengthfunctionrandom_strings($length_of_string){// String of all alphanumeric character$str_result='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';// Shufle the $str_result and returns substring// of specified lengthreturnsubstr(str_shuffle($str_result),0,$length_of_string);}// This function will generate// Random string of length 10echorandom_strings(10);echo"\n";// This function will generate// Random string of length 8echorandom_strings(8);?>chevron_rightfilter_noneOutput:hnQVgxd4FE 6EsbCc53
- Using md5() Function: The md5() function is used to calculate the MD5 hash of a string. Pass timestamp as a argument and md5 function will convert them into 32 bit characters
Example:
<?php// This function will return a random// string of specified lengthfunctionrandom_strings($length_of_string) {// md5 the timestamps and returns substring// of specified lengthreturnsubstr(md5(time()), 0,$length_of_string);}// This function will generate// Random string of length 10echorandom_strings(10);echo"\n";// This function will generate// Random string of length 8echorandom_strings(8);?>chevron_rightfilter_noneOutput:12945f0845 12945f08
- Using sha1() Function: This function calculates the sha-1 hash of a string. Pass timestamps as a argument and sha1() function will convert them into sha1- hash.
Example:
<?php// This function will return// A random string of specified lengthfunctionrandom_strings($length_of_string) {// sha1 the timstamps and returns substring// of specified lengthreturnsubstr(sha1(time()), 0,$length_of_string);}// This function will generate// Random string of length 10echorandom_strings(10);echo"\n";// This function will generate// Random string of length 8echorandom_strings(8);?>chevron_rightfilter_noneOutput:643f60c52d 643f60c5
- Using randon_bytes() Function: This function generates cryptographically secure pseudo-random bytes. It returns a string containing the requested number of cryptographically secure random bytes. Use binehex () function to convert bytes into hexadecimal format.
Example:
<?php// This function will return// A random string of specified lengthfunctionrandom_strings($length_of_string) {// random_bytes returns number of bytes// bin2hex converts them into hexadecimal formatreturnsubstr(bin2hex(random_bytes($length_of_string)),0,$length_of_string);}// This function will generate// Random string of length 10echorandom_strings(10);echo"\n";// This function will generate// Random string of length 8echorandom_strings(8);?>chevron_rightfilter_noneOutput:64713970f3 67b575a3
Recommended Posts:
- Program to generate random string in PHP
- Generate random alpha-numeric string in JavaScript
- PHP program to Generate the random number in the given range (min, max)
- How to generate random number in given range using JavaScript?
- Generating Random String Using PHP
- How to remove non-alphanumeric characters in PHP?
- PHP | ctype_alnum() (Check for Alphanumeric)
- JavaScript | Random
- How to get random value out of an array in PHP?
- p5.js | random() Function
- JavaScript | Get all non-unique values from an array
- JavaScript | Program to generate one-time password (OTP)
- How to get all unique values (remove duplicates) in a JavaScript array?
- Extract unique objects by attribute from array of objects.
- JavaScript | Difference between String.slice and String.substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



