Create a unique id from letters in php

7 replies
Hi guys...

Is it possible to generate a unique ascending id, based on letters.

Eg. aaaa, aaab, aaac, aaad, etc.

I want to have a string (aabb) and then via a function or something generate a new string (aabc).

If you have any ideas, let me know
#create #letters #php #unique
  • Profile picture of the author Tim_Myth
    Off the top of my head, I knwo you could use the ord() function to get the ascii value of the characters, then create a number from that, add 1, and create a string with the chr() function. Horribly inefficient though.

    If your string will ALWAYS only increment by one letter, and ALWAYS be sequential, you could make it more efficient by just grabbing the last character in the string, getting its ascii value, adding 1 to that, and then convert it back to a character.
    {{ DiscussionBoard.errors[3417703].message }}
    • Profile picture of the author hjalte81
      Originally Posted by Tim_Myth View Post

      If your string will ALWAYS only increment by one letter, and ALWAYS be sequential, you could make it more efficient by just grabbing the last character in the string, getting its ascii value, adding 1 to that, and then convert it back to a character.
      Thanks Tim...

      Eventually after a lot of searching, I got this to work:

      function new_uid($olduid){
      $number = base_convert($olduid, 36, 10);
      $newnumber = $number+1;
      $newuid = base_convert($newnumber, 10, 36);
      return $newuid;}
      {{ DiscussionBoard.errors[3417946].message }}
      • Profile picture of the author RyanAndrews
        You can increment strings directly in php.

        $x = 'AAA';
        $x++;
        echo $x;
        {{ DiscussionBoard.errors[3418903].message }}
        • Profile picture of the author jminkler
          What exactly are you trying to accomplish?
          {{ DiscussionBoard.errors[3418993].message }}
  • Profile picture of the author wayfarer
    I did this when I built my own url-minifier (like bit.ly). I started at 'a' and worked my way up to 'Z', after which I went to 'aa', 'ab' .... 'aZ', 'ba', etc. Anyway, here's how I did it:

    PHP Code:
    function increment($string) {
        for(
    $i 0$i strlen($string); $i++) {
            if(
    $string[$i] != 'Z') {
                
    $string[$i] = increment_single($string[$i]);
                break;
            }
            else {
                
    $string[$i] = increment_single($string[$i]);
                if(
    strlen($string) == $i 1) {
                    
    $string .= 'a';
                    break;
                }
            }
        }
        return 
    $string;
    }
    private static function 
    increment_single($char) {
        
    $dec ord($char);
        
    $dec++;
        if(
    $dec == 123) {
            
    $dec 48;
        }
        if(
    $dec == 58) {
            
    $dec 65;
        }
        if(
    $dec == 91) {
            
    $dec 97;
        }
        
    $char chr($dec);
        return 
    $char;

    For some reason I didn't even know that you can increment strings in PHP, though so I'm not exactly sure how that would work instead of this. Anyway, this way is tried and tested, and works exactly how I wanted it to.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[3419015].message }}
  • Profile picture of the author SteveJohnson
    PHP will increment strings, but it will not change case. IOW, when $a = "z", the next increment will not be "A", but "aa".
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3420269].message }}
  • Profile picture of the author herry
    A unique site ID can be created in PHP using the uniqid () function. This function has two parameters we can set. The first is the prefix. This is what will be appended to the beginning of each ID. The second is more_entropy. If this is false or not specified it will return 13 characters, if it is true then 23 characters will be returned.
    {{ DiscussionBoard.errors[3420748].message }}

Trending Topics