Jump to content

Aplhanumerical combination


unicoart

Recommended Posts

Hi,

i need to populate a mysql database with all alphanumerical combinations with length of 5(letters/numbers)

So:

$mix = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));

61^5 , i guess that would be 844 596 301 rows in table.

 

can anybody help me with this snippet?

 

Link to comment
https://forums.phpfreaks.com/topic/249899-aplhanumerical-combination/
Share on other sites

<?php

function permutations($letters,$num){ 
    $last = str_repeat($letters{0},$num); 
    $result = array(); 
    while($last != str_repeat(lastchar($letters),$num)){ 
        $result[] = $last; 
        $last = char_add($letters,$last,$num-1); 
    } 
    $result[] = $last; 
    return $result; 
} 
function char_add($digits,$string,$char){ 
    if($string{$char} <> lastchar($digits)){ 
        $string{$char} = $digits{strpos($digits,$string{$char})+1}; 
        return $string; 
    }else{ 
        $string = changeall($string,$digits{0},$char); 
        return char_add($digits,$string,$char-1); 
    } 
} 
function lastchar($string){ 
    return $string{strlen($string)-1}; 
} 
function changeall($string,$char,$start = 0,$end = 0){ 
    if($end == 0) $end = strlen($string)-1; 
    for($i=$start;$i<=$end;$i++){ 
        $string{$i} = $char; 
    } 
    return $string; 
} 

/* ************************************************* */

$mix = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
$mix = implode("", $mix);

$Array=permutations($mix,2); 
for($i=0 ; $i < count($Array) ; $i++) { 
        echo "$i." . $Array[$i] . "<BR>"; 
} 
?>

 

i think this will work

This would work:

 

require_once "incl/db_connections.php";

$rows = 1000;

function genCode($len = 5){
return substr(md5(rand(0,1000).time().microtime().rand(0,1000)), 0, $len);
}

for($i=0;$i<$rows;$i++){
$randCode = genCode();
mysql_query("insert into my_table (code) values ('$randCode')");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.