Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit the md5() function PHP to generate 8 random caharacters and numbers?

Tags:

php

How can I limit the following code to generate 8 mixed characters and number in the code below.

Here is the PHP code.

md5(uniqid(rand(1,6)));
like image 894
stick Avatar asked Dec 09 '22 14:12

stick


1 Answers

A md5 hash is always 32 characters long ; that's what an md5 is.

But you could choose to use only 8 characters from the string returned by the md5() function, with the substr function.


For example, to keep only the first eight characters, you could use something like this :

echo substr(md5(uniqid(rand(1,6))), 0, 8);

And it would get you something like this :

4651da2b
like image 176
Pascal MARTIN Avatar answered May 14 '23 17:05

Pascal MARTIN