Jump to content

Recommended Posts

I have asked this on god knows how many forums, and so far no answers. But hopefully you guys will be able to help me.

 

I was wondering how I would generate a set of Alphanumeric strings with PHP. So the script would run and output something like:

 

a

b

c

d

..

x

y

z

aa

ab

ac

ad

...and so on.

 

Alpha would be good, although Alphanumeric would be much better. Also the option to define how many characters long the string can be would be a plus. If anyone can show me how this is done, or even an idea on how I could do it, I would be very happy. All help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/78629-sequential-alphanumeric-string-generator/
Share on other sites

<?php
$i = 0;

$num_rows = 3;
while($i < $num_rows){
$j = 97;
while($j <=122){
echo chr($j)."<br />>";
$j++;
}
$i++;
?>

It needs a bit of work btu that will be your general solution, you will need to apply a setting using dynamic counters in the while $j <= 122 some sort of function to get a character, i got an idea give me a sec

<?php

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

   function alphagen($len) {
       foreach (range(1,$len) as $i) {
           echo num2alph($i) . "\n";
       }  
   }  

   alphagen(100);

?>

I tried something like this (it does first 100)

http://pira00.worldispnetwork.com/num.php

 

It only supports up to zz so it would need to be coninuted, but the idea is

 

<?php
$i = 0;
$num_rows = 100;
$j = 97;
$rows = array();
while($i <$num_rows){
$row[$i] = char_gen($i);
$i++;
}

function char_gen($num){
if($num>25){
	$firstchr = floor($num/26)-1;
	$string[0] = chr($firstchr+97);
	$secondchr = $num%26;
	$string[1] = chr($secondchr+97);
}
else{
	$string[0] = chr($num+97);
}
return $string[0].$string[1];
}
print_r($row);
?>

 

  Quote

<?php

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

    function alphagen($len) {
        foreach (range(1,$len) as $i) {
            echo num2alph($i) . "\n";
        }  
    }  

    alphagen(100);

?>

 

I modified that to

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

So that I can define an offset (so alphagen(11, 11) will return the 11th character that can be generated)

 

How can I make the variable $string accessible outside of the function?

 

EDIT:

 

Never mind, I was doing it wrong

 

EDIT2:

 

Wait, no. I still need the character generated to be a variable that can be accessed outside of the function

It really depends on how you want it. It probably best to have it return an array.

 

<?php

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

$array = alphagen(10,45);

?>

 

Othewise, you could have one string returned with each element seperated by a space. eg;

 

<?php

function alphagen($start, $len) {
  $return = '';
  foreach (range($start ,$len) as $i) {
    $return .= num2alph($i) . " ";
  }
  return $retun;
}

$string = alphagen(10,45);
echo $string;

?>

 

An array is probably more usefull though.

You could even make it return an array by default, or optionally a string.

 

<?php

function alphagen($start, $len, $string=false) {
  $return = '';
  foreach (range($start ,$len) as $i) {
    if ($string) {
      $return .= num2alph($i) . " ";
    } else {
      $retun[] = num2alpha($i);
    }
  }
  return $retun;
}

$string = alphagen(10,45,true);
echo $string;

?>

  • 1 month later...

Sorry to bump my thread, but I was wondering how I could make the function generate strings with capital letters and numbers.

 

So it could work like

 

0

1

2

...

9

a

b

c

d

...

z

A

B

C

 

and so on

 

I have tried everything I can think of, but I simply can't get it to work.

  • 4 years later...

The good old

 

for ($i=0;$i<$len;$i++) {

 

would seem to be better than

 

 foreach (range($start ,$len) as $i) {

 

because the latter has to generate an array of size $len, which severely limits how far you can go due to the increasing memory requirements. The regular for loop causes no increase in memory use no matter how big $len is (at least under PHP_INT_MAX).

 

Also, instead of

 

$anum = chr(($num % 26)+65).$anum;
...
return strtolower($anum);

 

you could skip the strtolower() simply by moving the chr() offset to 97 (where lower case letters start in ASCII) instead of 65 (where upper case letters start):

 

$anum = chr(($num % 26)+97).$anum;
...
return $anum;

Edited by KeithTyler

 function num2alph($num) {
 $anum = '';
 while($num >= 1) {
	 $num = $num - 1;
	 if (($num % 52) >= 26) {
		 $anum = chr(($num % 26)+65).$anum;
	 } else {
		 $anum = chr(($num % 26)+97).$anum;
	 }
	 $num = $num / 52;
 }
 return $anum;
}

 

will generate strings containing both lower and upper-case letters, e.g.:

 

aa

Aa

AA

Ab

aB

Edited by KeithTyler

You can increment characters in PHP

 

<?php
$var = 'a';

for ($i = 0; $i < 260; $i++) {
   if ($i % 26 == 0) echo '<br>';
   echo $var++ . ' ';
}
?>

 

RESULTS:

 

a b c d e f g h i j k l m n o p q r s t u v w x y z

aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az

ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz

ca cb cc cd ce cf cg ch ci cj ck cl cm cn co cp cq cr cs ct cu cv cw cx cy cz

da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz

ea eb ec ed ee ef eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex ey ez

fa fb fc fd fe ff fg fh fi fj fk fl fm fn fo fp fq fr fs ft fu fv fw fx fy fz

ga gb gc gd ge gf gg gh gi gj gk gl gm gn go gp gq gr gs gt gu gv gw gx gy gz

ha hb hc hd he hf hg hh hi hj hk hl hm hn ho hp hq hr hs ht hu hv hw hx hy hz

ia ib ic id ie if ig ih ii ij ik il im in io ip iq ir is it iu iv iw ix iy iz

 

After 'zz' it will go to 'aaa'

Guest
This topic is now 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.