Unable to restrict unique random generation to required output

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?

Upvotes: 0

Views: 18

Answers (1)

0stone0
0stone0

Reputation: 44162

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;

Online demo

Upvotes: 1

Related Questions