Converting Numeric Data to Alphanumeric in PHP with base_convert
Published: January 25, 2018
Recently a client came to me with the following request…
We’re planning publish “offer codes” in our print circulars so that customers can search for more details on our website. The offer codes our system generates are long and will be cumbersome for users to enter into the search field on the website. What can we do about this?
The offer codes looked something like this…
I thought about it a little and then a lightbulb went off in my head
This could be solved by stripping the “PRE-“ (which was consistent across every offer code) and converting the remaining numeric data to alphanumeric via a base-10 to base-36 conversion. The result would be short alphanumeric codes that looked something like this…
In this post we’ll look at how this can be achieved in PHP.
Converting from numeric to alphanumeric is very easy in PHP. The
base_convert function is designed to do exactly this.
Here’s the signature for the function…
string base_convert( string $number, int $frombase , int $tobase )
base_convert allows for conversion beginning from base-2 (binary) all the way to base-36 (alphanumeric).
This is what the implementation looked like for me…
$offerId = 'PRE-0019003400'; $alphanumeric = base_convert($offerId, 10, 36);
A couple things worth noting…
- It is not necessary to manually strip off the “PRE-“ at the beginning of the offer code (e.g. via
substr) as PHP will automatically strip out any non base-10 characters when converting from base-10.
base_convertis case insensitive with alpha characters, but will generate lowercase when going from base-10 to base-36.
strtouppercan be used if uppercase alpha characters are desired.
base_convertis not safe with very large numbers. In my case the largest possible number (in decimal) was 9999999999, which is a piece of cake for
base_convert. However, if you’re working with very large numbers,
base_convertis not a good option.