You are invited to Log in or Register a free Frihost Account!

Incrementing Strings

Star Wars Fanatic
I know that you can increment Strings in php using the standard ++, such as this:
$x = "a" ;
$x ++ ;
// $x now = "b"

But I need a way to increment and include more than the plain ASCII characters (a-z, A-Z).
I need to include characters 0-9 and "%".
An example would be:

$x = "z" ;
$x = increment ( $x ) ;   // $x now = "0"
$y = "9"
$y = increment ( $y ) ;   // $y now = "%"

Any suggestions? Could I use Regex? And then would it be slow? I only need to to increment the last character, so I could have two or more such as "az" and incrementing would equal "a0".
And it needs to carry so that "a%" incremented equals "ba".

I need to be fast, as I'm going to be looping through them, and I don't want it to take a long time, relatively speaking.
Increments strings from the ascii range 0x21 to 0x7f

function increment($str){
   for($pos = strlen($str) - 1;$pos >= 0;$pos--){
      $str[$pos] = chr($n = ord($str[$pos]) + 1);
      if($n > 0x7f)
         $str[$pos] = chr(0x21);
   return $str;

$str = 'aaa';
for($a = 0;$a < 22000;$a++)
   echo ($str = increment($str)) . "\n";
If you don't mind me asking, why are you doing this?

I'm not saying that there isn't a place for this but; in most languages you can't increment strings for the very reasonable argument that it doesn't make logical sense. Why use letters when you are doing something that really only applies to numbers?

If you look at the above solution, you've re-implemented something done at the bit-level in a massively inefficient way.
I assumed it was a php class assignment. Which is why it has almost no real world application. About the only use would be to create unique strings, but there are better ways.

I edited the code by combining 2 lines into one.
It doesn't do anything at the bit level.
Star Wars Fanatic
That code doesn't work right, but it did lead me along the right path, thanks jmraker.

And as for the purpose, no it's not for a class, it's more of a curiosity. There a website that uses an id to get to it, instead of the name, and I want to index all of the ids and see if I can figure out how they encrypt the name.
It's really slow, as most ids are 10 characters or longer, and I have to load each page to check if they exist.
Fair enough, I'd be interested to see the solution if you ever work it out.

Have you considered that it might be encoded in Base64? That's the most obvious thing to implement I could think of given the range you specified. Not sure about the "%" though.

It doesn't do anything at the bit level.

Sorry, that wasn't meant to come across like a criticism of your code and rather why (in most cases) a design like this probably a bad idea.

What I meant was that instead of using integers which are then incrememted bit-by-bit by decidcated CPU instructions. Doing anything involving character strings like this means that you are using many bytes (probably two) to represent the same thing and manually implementing the arithmetic. Unsurprisingly, this is far less efficient than just letting the hardware do its basic operations.
Star Wars Fanatic
Yea, I've considered a few things, at this point, I'm not even sure they do encode it, as it seems to come in pairs, each begins with the same 8 characters, between 18 and 28 characters long, it's very strange.
There are also a few other characters I've noticed, such as (.

And I used a different method then looping through each one, though I did use the code. Thanks for your help.
Related topics
Question about MySQL!
[php scripts ] phpweather&email
The Unofficial Jokes Thread
4 humors
pay attition to the new virus Backdoor.Nibu.K
Guitar Thread: 7 Strings Guitars (or possibly more)
favorite guitar strings?
Connection strings !
colored guitar strings.
Strings - A Pakistani Band
Bass strings
php need help:how to replace these strings ?
Escaping Strings For Data Insertion
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.