6.7 C
New York
Wednesday, March 22, 2023

How to generate random alphanumeric string in PHP?





In this example i am going to show you how to generate randomly alphanumeric string OR number in PHP. when you need you generate random alphanumeric string for your unique field then you can easily create using substr(), md5(), rand(), uniqid(), mt_rand(), time() and str_shuffle() php function you can create several way unique code using following example:

Example

Example 1

$generated = substr(md5(rand()),0,5);

print_r($generated);

Example 2

$generated = rand(10000,99999);

print_r($generated);

Example 3

$generated = substr(md5(uniqid(rand(), true)),0,5);

print_r($generated);

Example 4

$generated = substr(time(),5);

print_r($generated);

Example 5

$generated = substr(str_shuffle(md5(time())),0,5);

print_r($generated);

Example 6

$generated = substr( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,mt_rand( 0 ,50 ) ,2 ) .substr( md5( time() ), 1,3);

print_r($generated);

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles