Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8109113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:19:11+00:00 2026-06-06T01:19:11+00:00

I would like to build a php script that automatically generates a new id

  • 0

I would like to build a php script that automatically generates a new id by increasing the previous by 1.
eg: A0009 becomes A0010 and A9999 becomes B0000

I have written one that works but it doesn’t go over 5 chars long:
eg: Z9999 should go to A00000 and so on.

Any suggestions?
here is my snippet:

<?php
function replaceChar($string2replace)
{
$charLength = strlen($string2replace)-1;
$charAt = array();
$charAt[4] = substr($string2replace, -1);
$charAt[3] = substr($string2replace, -2,1);
$charAt[2] = substr($string2replace, -3,1);
$charAt[1] = substr($string2replace, -4,1);
$charAt[0] = substr($string2replace, 0,1);

if($charAt[4] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[4]+1,$charLength);
}
else
{
$charAt[4] = 0;
$string2replace = substr_replace($string2replace,$charAt[4],$charLength);
    if($charAt[3] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[3]+1,$charLength- 1,1);
}
else
{
$charAt[3] = 0;
$string2replace = substr_replace($string2replace,$charAt[3],$charLength-1,1);

if($charAt[2] < 9)
{
$string2replace =  substr_replace($string2replace,$charAt[2]+1,$charLength-2,1);
}
else
{
$charAt[2] = 0;
$string2replace = substr_replace($string2replace,$charAt[2],$charLength-2,1);

if($charAt[1] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[1]+1,$charLength-3,1);
}
else
{
$charAt[1] = 0;
    $string2replace =    substr_replace($string2replace,$charAt[1],$charLength-3,1);
    }

if($charAt[0] < 'z')
{
$charAt[0] ++;
$string2replace =    substr_replace($string2replace,$charAt[0],$charLength-4,1);
}
else
{
$charAt[0] = 'a';
$string2replace =  substr_replace($string2replace,$charAt[0],$charLength-4,1);
}
}   
}
}
return $string2replace;
}

$string2begin = 'A9999';

$generatedString = replaceChar($string2begin);

echo $string2begin . "<br />" . $generatedString;


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:19:12+00:00Added an answer on June 6, 2026 at 1:19 am

    Your ID numbering scheme seems rather contrived, where the high-order digit is A-Z and the remaining digits are 0-9. If I understand that pattern correctly, this seems to do the trick:

    function incrementID($id)
    {
        $letter = $id[0];
        $number = substr($id, 1);
    
        $newNum = str_pad($number + 1, strlen($number), '0', STR_PAD_LEFT);
    
        // increase number only
        if (strlen($number) == strlen($newNum))
            return $letter . $newNum;
    
        // increase ID length ('Z' to 'A')
        if ($letter == 'Z')
            return 'A' . str_repeat('0', strlen($number) + 1);
    
        // change letter
        $newLetter = chr(ord($letter) + 1);
        return $newLetter . str_repeat('0', strlen($number));
    
    }
    
    printf("%s\n", incrementID('A0009')); // 'A0010'
    printf("%s\n", incrementID('A9999')); // 'B0000'
    printf("%s\n", incrementID('Z9999')); // 'A00000'
    

    Even though your examples didn’t fit this, I first assumed you really just wanted a base-36 number (any digit could be 0-9,A-Z, where A is 10 and Z is 35). Working with numbers in base-36 is easy because you can use base_convert() to convert them to customary base-10. This is all you would need to do to increment base-36 numbers:

    function incrementBase36($id)
    {
        $numVal = base_convert($id, 36, 10);
        $newId = base_convert($numVal + 1, 10, 36);
        return strtoupper($newId);
    }
    
    printf("%s\n", incrementBase36('A0009')); // 'A000A'
    printf("%s\n", incrementBase36('A9999')); // 'A999A'
    printf("%s\n", incrementBase36('Z9999')); // 'Z999A'
    printf("%s\n", incrementBase36('AZZZZ')); // 'B0000'
    printf("%s\n", incrementBase36('ZZZZZ')); // '100000'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to build up a new IDocument object step by step using
I would like to build a web application that heavily relies on the Yodlee
I would like to build a query that will pull the number of rows
I'm hacking together a simple php script that will build a list of photo
I would like to make a php script output like a real 404 page
I'd like to build a simple php script to cache remote JSON files every
I'm looking to build a PHP script that parses HTML for particular tags. I've
I would like to build a cypher with python which decoding text by repeatedly
I would like to build modules via an aggregator, but avoid the aggregator project
I would like to build two different versions of my app using different android:minSdkVersion.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.