Generate code PHP

hidrau
hidrau used Ask the Experts™
on
Hello Guys

I'd like a function where I could generate key code like this: atl1457b01, in randon way.

akq7485v17
ets7258f85
pqt1975g74

Someone has a function that can do that?

Thanks a lot
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
What is the application - is it for security reasons

What are the requirements for characters / min / max length?
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Freelancer
Top Expert 2010
Commented:
You can use something like this:
function generate_random_code()
{
	$letters = 'abcdefghijklmnopqrstuvwxyz';
	$numbers = '0123456789';
	$code = '';
	for ( $i = 0; $i < 3; $i++ )
	{
		$code .= $letters[ rand( 0, strlen($letters) - 1 ) ];
	}
	for ( $i = 0; $i < 4; $i++ )
	{
		$code .= $numbers[ rand( 0, strlen($numbers) - 1 ) ];
	}
	$code .= $letters[ rand( 0, strlen($letters) - 1 ) ];
	for ( $i = 0; $i < 2; $i++ )
	{
		$code .= $numbers[ rand( 0, strlen($numbers) - 1 ) ];
	}
	return $code;
}
echo generate_random_code();

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:

Author

Commented:
thanks a lot

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial