Obfuscation challenge

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Obfuscation challenge

Post by alex.barylski »

I've given a quick attempt:

Code: Select all

function generateUniqueId()
{
  static $entry = 0;
  static $index = 0;

  $valid = array('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789');

  if($entry == 0){
    $value = $valid{0}; // First time funciton is called make sure we use lower case 'a' as the first character
    $entry = 1;
    $index = 1; // Use next character in array next call
  }
  else{

    // NOTE: I'm starting to become discombobulated - so I'll leave it here 
    if($index < strlen($valid) && $depth){
      $value = $valid{$index};
    }
      
    $index++;
  }
  
  return $value;
}
So the source is incomplete...but it demonstrates the idea...

Basically the idea is to generate a unique variable but instead of generating lengthly ID's such as 7EH3DHBFYRF7RBFVGHY I want to generate names in the following pattern:

a
b
c
d
e
..
_

Then...

aa
ab
ac
ad
ae
af
ag
ah
ai

Then...

ba
bb
bc
bd
be
bf
bg
...

Then...

ca
cb
cc
cd
ce
...

You get the idea.

I'm curious to see what others might come up with...

p.s-I use statics because the unique ID is generated on each call to the function.

Anybody?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

a faster (albeit less compact) solution would be to use:

Code: Select all

function generateUniqueId(){
  static $var = 'a';
  return $var++;
}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Haha...

Thats what I used initially. The variable names grow to tremdous size though, so I scrapped the idea. :P

p.s-I didn't think this message made it into DevNet. With all the errors I've been getting lately about half my messages don't take the first time round so I just forget it and pretend it never happened.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

the code I posted is still pretty compact, it produces:

Code: Select all

a
b
c
.
.
.
x
y
z
aa
ab
ac
.
.
.
ay
az
ba
bb
.
.
.
The only difference is the lack of capitals and digits.
Post Reply