-3

I have to pass an index to a function and from that index return a string containing from 1 to 4 chars.

I have:

$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

So if I say index(20) the function should return lower "k" because it is its index in the $string variable.

But...

If I type indexes

62 it should return 10
63 .. 11

That is because from index 62 it should start the string from 1, not 0, and loop until the next index, so they will be 2 length string

If I type indexes

3843 .. ZZ

That is because from index 3843 all the possibilities from [0-9a-zA-Z][0-9a-zA-Z] have ended and now the string starts with 3 length.

3844 .. 100

...

9999 .. 2Bh

All the possibilities and at [0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z] but I only need until index 9999

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
Gilberto Albino
  • 2,572
  • 8
  • 38
  • 50
  • and why do u need to do this?? – swapnesh Aug 24 '12 at 11:38
  • and what is the reason of that, you want to access all permutations of string? Maybe this will help you http://stackoverflow.com/questions/2617055/how-to-generate-all-permutations-of-a-string-in-php – Luke Adamczewski Aug 24 '12 at 11:39
  • wow, that's seriously over-complicated. Took me a while to work out what you were asking for, and when I did work it out, I still couldn't work out why. What on earth are you trying to achieve with this? – SDC Aug 24 '12 at 11:56
  • by the way, for anyone else trying to work out what he's asking for, it helps to know that 62 is the length of the string, and 3844 is 62 squared. – SDC Aug 24 '12 at 11:58
  • At the end of the day, it's just a base conversion between base 10 and base 64. Still can't understand why you'd want to do it that way. Your client probably thinks he's being clever and coming up with something new, but it isn't really. Crazy, yes, but not new. – SDC Aug 24 '12 at 12:15
  • SDC, I found why they were going to need that... the ID to identify the URL will be the primary key index, both on file system based backup and database storage. It's really a strange usage! – Gilberto Albino Aug 25 '12 at 21:45

2 Answers2

2

Your question is a bit hard to understand, not sure if this is what you want:

$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 

function t($n)
{
    global $string;
    $ret = $string{$n%62};
    if($n>=62)
        $ret = t(floor($n/62)).$ret;
    return $ret;
}

echo t(9999);
agou
  • 728
  • 1
  • 10
  • 24
0

What you're asking for is basically a number base converter, going from base 10 (decimal) numbers to base 64 (using your string as the 64 digits in counting system).

PHP does provide a built-in base_convert() function, however while it works well for converting between say hexadecimal and decimal, it doesn't work for very high bases like this. This is partly because no-one generally has any need for them, and partly because the digits of such a base are not an agreed standard.

There are several examples in the comments of the base_convert() page linked above where people have written functions that do attempt to work for high bases. I can't vouch for any of them though.

SDC
  • 14,192
  • 2
  • 35
  • 48