How to increment letters like numbers in PHP?

后端 未结 8 1373
孤城傲影
孤城傲影 2020-11-27 16:41

I would like to write a function that takes in 3 characters and increments it and returns the newly incremented characters as a string.

I know how to increase a sing

8条回答
  •  长情又很酷
    2020-11-27 16:46

    You are looking at a number representation problem. This is base24 (or however many numbers your alphabet has). Lets call the base b.

    Assign a number to each letter in alphabet (A=1, B=2, C=3).

    Next, figure out your input "number": The representation "ABC" means A*b^2 + B*b^1 + C*b^0 Use this formula to find the number (int). Increment it.

    Next, convert it back to your number system: Divide by b^2 to get third digit, the remainder (modulo) by b^1 for second digit, the remainder (modulo) by `b^0^ for last digit.

    This might help: How to convert from base10 to any other base.

提交回复
热议问题