php random string, php unique string, php shuffle string, php md5, generate 4 digit number in PHP,Generate alphanumeric random and unique string in PHP

Generate alphanumeric random and unique string in PHP

Mostly while using different applications daily we sometimes come across a situation where we are required to input a code sent either to your email or mobile phone which either comprises of four or five or six characters or numbers that expires after a certain period of from one minute to sixty minutes or up to twenty-four hours.

This code generation either follows a certain sequence or is sometimes random. Another scenario is when you are signing up to a certain application and you are not requested for the password, a password is sent to your email or through SMS which you are required to change whenever you log in for the first time. Also, during forgot or reset password, a code is sent to your email to confirm whether you are truly the owner of the account.

All of the above scenarios follow the procedure of generating either an alphanumeric random and unique string which is simply easy to achieve while using the PHP language. Therefore, in this article, we shall focus on how to generate an alphanumeric string using PHP, how to generate a random string using PHP, how to generate a unique string using PHP, and show examples of how this is done.

PHP provides various inbuilt functions that help us achieve how to generate unique random strings.

What is an alphanumeric string?

An alphanumeric string is a string that contains both numbers (0 to 9) and alphabet letters (A to Z) both uppercase and lowercase. An example of an alphanumeric string is A0Bx3EewK2

What is a random string?

A random string is a string that does not follow any order or sequence, that is, given a set of numbers or letters it shuffles it in any direction and any order

What is a unique string?

A unique string is a string that stands out from other strings, for example, your ID number or passport number is unique and distinct such that while it’s being created there has to be some check not to duplicate. Mostly, unique strings follow a certain sequence.

While generating either an alphanumeric string or a random string or a unique or one string that has all the three combined, you must first define the length of the string you want to generate or create, and also you must have a set of numbers or characters you are generating the string from.

Generating an alphanumeric string

The length of the string will be 10

The string set will be 0 to 9, A to Z and a to z

We use a PHP in built function

  1. str_shuffle() function

<?php

$length = 10;

$str = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';

echo substr(str_shuffle($str), 0, $length);

?>

The result of the above code will be a string with 10 characters like shown below

8egOHrK6uq

When you execute the code another time, it will produce a different result since the function does not follow any order therefore it generates an alphanumeric random and unique string.

You can generate the string you wishes as you can remove any set of numbers, that is, you can remove the numbers and your result will not have numbers included or remove the lowercase characters

Generating uppercase random unique string

<?php

$length = 10;

$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

echo substr(str_shuffle($str), 0, $length);

?>

The result will be like

UAYSTJVGLI

Generating a lowercase random unique string

<?php

$length = 10;

$str = 'abcefghijklmnopqrstuvwxyz';

echo substr(str_shuffle($str), 0, $length);

?>

The result will be like

ybgrqlzhja

Generating a number random unique value

Note that you can update the length, in this case will change to 6

<?php

$length = 6;

$str = '0123456789';

echo substr(str_shuffle($str), 0, $length);

?>

The result will be like

058647

   2. Using md5 function

The md5 function returns the hash value of the string or the number

The result of the string generated does not change hence it is mostly applicable while strong passwords

You can define the length or leave it. The length is used to limit the result to a certain number of characters

<?php

$length = 6;

$str = '0123456789';

echo substr(md5($str), 0, $length);

?>

The result will be

781e5e

 

Conclusion

We have discussed two ways of how you can generate a random alphanumeric unique string using PHP language and shown some examples. You can generate more examples following the structure we have discussed above.