0

I'm trying to generate 6 character long alpha numeric string in php.

I'm getting question and answers on stackoverflow.

shorturl on stackoverflow

Random string generate in php but not unique string

Random, unique string generate in php but didn't unique at time with 6 length of string

Can I generate more 1 Lacs string at time,

$arr = [];
$matchStr = [];
for($i=1;$i<=10000;$i++){
    $saltString = "ap_";
    $dynamicStr = substr(md5($saltString.$i), 26, 32);
    do{
       $arr[$i] = $dynamicStr;
    }while(!in_array($dynamicStr,$arr));
}

I'm generate strings possible to all ways but didn't get unique string and store into mysql table with unique index of column.

I know possibility of generate of string is:

$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

Random String Possibility: length of string: 10

a-z = 26

A-Z = 26

0-9 = 10

(26+26+10)^10 = 8.3929937 x 10^17 possibility at time.

is that any solution for generate string at time? in laravel trying with below code

function getrandomstr() {
    $length = 6;
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = "";

    for ($p = 0; $p < $length; $p++) {
       @$string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}
ini_set('max_execution_time', 0);
$arr = [];
$matchStr = [];
$query = [];
for($i=1;$i<=100000;$i++){
   $str = getrandomstr();
   do{
      $query[] = [
          "hashcode" => $str
     ];
   }while(\App\Model::where('hashcode',$str)->count() == 0);
}
foreach(array_chunk($query,5000) as $a){
    \App\Model::insert($a);
}
Community
  • 1
  • 1
Dhaval Tailored
  • 386
  • 2
  • 6

3 Answers3

0
function getrandomstr() {
    $length = 6;
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = "";

    for ($p = 0; $p < $length; $p++) {
        @$string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
Salman
  • 67
  • 4
  • @DhavalTasol: It does generate a random string within the constraints you have defined (6 character, alpha-numeric). It cannot return a MySQL error, since it doesn't use MySQL - that error comes from somewhere else in your code. – Samuel Lindblom Mar 28 '16 at 08:39
  • Your code is incorrect! It might return shorter strings due to wrong using of `mt_rand`. The rand function may return any integer from 0 to max (`strlen($characters)` in this case) including, but `strlen($characters)` is greater than the max index of `$characters` string (since indexes start from 0). The correct line will be: `$string .= $characters[mt_rand(0, strlen($characters) - 1)];` – fremail Oct 27 '18 at 15:07
0

If you want an unique identifier for each row in a database, use a simple automatically incrementing integer. If you don't want an integer use the UUID() function in MySQL, which will give you a complex string that is practically unique.

If you want to be able to generate a random string within strict constraints (6 character alpha-numeric) that is guaranteed to be unique, you will need to compare it to all the existing keys in the database before/while inserting it. Just using a random string generator will eventually generate a duplicate since there is a finite set of results, how often depends on your constraints.

Samuel Lindblom
  • 812
  • 1
  • 6
  • 22
0

I suggest you take a look at Hashids. It's a simple laravel package (well, a wrapper for http://hashids.org/).

After you add the package go to config and change salt and the rest to this

'main' => [
    'salt' => 'your-salt-string',
    'length' => '6',
    'alphabet' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
],

And simply use like this:

Hashids::encode(123456);
Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57