Jump to content

help me fix my leeched botched code


ssjrocks

Recommended Posts

so i got 2 snippets

the first works exactly as i need it to except its limited to lowecase ..

im using a for loop that calls it like this

 

$line =  alphagen($i,$i);

 

the bottom function works has a bigger range .. (and i can understand it .. i added the special chars bit)

but it generates randoms with collisions

is there a way to modify the first 2 functions to make it use a list or even an array so that i can interchange the charlist easier ( from form post)

my favouriite would be if the second function could me modified to do pretty much what its doing now but in a sequential mannner

 

eg

echo  alphagen(1);    = a

echo  alphagen(2);    = b

echo  alphagen(bignumber); = Arfg4_2

 

 

  
  function num2alph($num) {



        $anum = '';
        while($num >= 1) {
            $num = $num - 1; 
            $anum = chr(($num % 26)+65).$anum;
            $num = $num / 26;
        }  
      return strtolower($anum);
    }  




    function alphagen($start, $len) {



        foreach (range($start ,$len) as $i) {
         $string = num2alph($i);
        return $string;
        }  
    } 



******************************************************************




function makepin($lenth) {
    // makes a random alpha numeric string of a given lenth
  $spec = array("0" => " ", "1" => "_", "2" => ".", "3" => "-");     
    $aZ09 = array_merge(range('A', 'Z'), range('a', 'z'),range(0, 9),$spec);
    $out ='';
    for($c=0;$c < $lenth;$c++) {
       $out .= $aZ09[mt_rand(0,count($aZ09)-1)];
    }
    unset($az09,$lenth,$c);
    	gc_collect_cycles();
    return $out;
}

Link to comment
https://forums.phpfreaks.com/topic/253684-help-me-fix-my-leeched-botched-code/
Share on other sites

and i would love to know what this does??

i am not that good at maths and got lost at the explanation of % as a math operator

while($num >= 1) {
            $num = $num - 1; 
            $anum = chr(($num % 26)+65).$anum;
            $num = $num / 26;
        }  

 

Probably not the best way to explain it but that is what it means

spot on man i get it now

still spinnin out on that calculation to get the ascii codes ...

 

i basicly need to be able to call $line = rangegenerator($i);

it doesnt matter if the number associated to each char changes each time a diffrent charset is used

 

so i made my own solution...

 

 

function num2alph($num) {
     	$space[0] = ' ';
     	$spec = array("0" => "_", "1" => ".", "2" => "-"); 
$aZ09 = array_merge($space, range('A', 'Z'), range('a', 'z'),range(0, 9), $spec);
        $anum = '';
        while($num >= 1) {
            $num = $num - 1; 
            $anum = $aZ09[($num % count($aZ09))].$anum;
            $num = $num / count($aZ09);
        }  
        return strtolower($anum);
    }  

    function alphagen($start, $len) {
        foreach (range($start ,$len) as $i) {
         $string = num2alph($i);
        return $string;
        }  
    }

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.