I want to restrict the unique numbers to be only 8 characters long with my prefix. Here is what i do
$prefix = "CEVO";
$u_unique_id = str_shuffle(md5(uniqid(rand(100000,999999), true)));
$user_unique_id = $prefix.$u_unique_id;
But this code isn't restricting to 8 characters. Where i am wrong?
CodePudding user response:
PHP md5()
:
Returns the hash as a 32-character hexadecimal number.
Use substr()
to limit that to the desired length, so in your case 8 - 4 = 4
;
<?php
$prefix = "CEVO";
$u_unique_id = str_shuffle(md5(uniqid(rand(100000,999999), true)));
$user_unique_id = $prefix . substr($u_unique_id, 0, 4);
echo $user_unique_id;