Kishan Kishan - 4 months ago 9
PHP Question

Unique key generate with digit and letters in php

I need to generate unique key in php.

The key must be min 8 digit and 4 letters contain.

So any one have idea ?

Thanks you

Answer

Try this one

$digit_length=8;
    $chars_length=4;

    $digit ="1234567890";
    $chars ="abcdefghijklmnopqrstuvwxyz";
    $final_rand=array();
    for($i=0;$i<$digit_length; $i++)
    {
        $final_rand[]= $digit[ rand(0,strlen($digit)-1)];
    }
    for($j=0;$j<$chars_length; $j++)
    {
        $final_rand[]= $chars[ rand(0,strlen($chars)-1)];
    }   

    $new_array = $final_rand;
    shuffle($new_array);

    for($i=0;$i<count($new_array);$i++)
    {
        echo $new_array[$i];
    }