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 single letter to the next one but how would I know when to increase the second letters and then stop and then increase the first letter again to have a sequential increase?
So if AAA is passed, return AAB. If AAZ is passed return ABA (hard part).
I would appreciate help with the logic and what php functions will be useful to use.
Even better, has some done this already or there is a class available to do this??
Thanks all for any help
You could use the ASCII codes for alpha numerics. From there you increment and decrement to get the previous/next character.
You could split your string in single characters and then apply the transformations on these characters.
Just some thoughts to get you started.
As proposed in PHP RFC: Strict operators directive (currently Under Discussion):
Whether or not the RFC gets merged, PHP will sooner or later go that direction of adding operator strictness. Therefore, you should not be incrementing strings.
a-z/A-Z ranges
If you know your letters will stay in range a-z/A-Z (not surpass z/Z), you can use the solution that converts letter to ASCII code, increments it, and converts back to letter.
Use
ord()
achr()
:ord()
converts the letter into ASCII num representationchr()
the number gets converted back to the letterAs discovered in comments, be careful. This iterates ASCII table so from
Z
(ASCII 90), it does not go toAA
, but to[
(ASCII 91).Going beyond z/Z
If you dare to go further and want
z
becameaa
, this is what I came up with:I'm giving you also 100% coverage if you intend to work with it further. It tests against original string incrementation
++
:Character/string increment works in PHP (though decrement doesn't)
You can do it with the ++ operator.
aba
However this implementation has some strange things:
This will print out letters from
a
toy
.This will print out lettes from
a
toz
and it continues withaa
and ends withyz
.To increment or decrement in the 7bits 128 chars ASCII range, the safest:
So, it is normal to get a backtick by decrementing
a
, as the ascii spec listPrint the whole ascii range:
More infos about ANSI 7 bits ASCII:
man ascii
To increment or decrement in the 8-bits extended 256 chars UTF-8 range.
This is where it starts to differ regarding the host machine charset. but those charsets are all available on modern machines. From php, the safest is to use the
php-mbstring
extension: https://www.php.net/manual/en/function.mb-chr.phpMore info, as example:
man iso_8859-9
Example, we can find the
€
symbol in ISO 8859-7:To increment or decrement in the 16 bits UTF-16 Unicode range:
Here is a way to generate the whole unicode charset, by generating html entities and converting to utf8. Run it online
Same stuff, but the range goes up to
(16^4 * 4)
!To retrieve the unicode
€
symbol,using the base10 decimal representation of the character.The same symbol, using the base16 hexadecimal representation:
First 32 bits are reserved for special control characters, output garbage �����, but have a meaning.