Home > Programing, Web Development > Convert Decimal into Alphanumeric using php

Convert Decimal into Alphanumeric using php


A Problem occurred when I was writing a script for shortUrl redirection. That using 5 Digit decimal numbers can cover only 105=100000 Urls but 5 digit alphanumeric (case sensitive) numbers are able to cover 622=916132832. So it is very helpful to keep Urls short.

Example

  • Using Decimal http://surl.bz/0 to http://surl.bz/99999 only 100000 Urls In 5 Digit
  • Using alphanumeric http://surl.bz/0 to http://surl.bz/ZZZZZ 916132832 Urls that’s Great choice


You can use this script to convert any decimal number into case sensitive alphanumeric number. This script is useful to create short profile id for you registered user like http://www.yourdomain.com/profile/AS34G or any unique id while keeping Url short.

<?php
function alphanumeric($dec)
{
$an="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$id="";
$base=62;
while($dec>=$base)
{
$mod=$dec%$base;
$id=$an[$mod].$id;
$dec=$dec/$base;
}
$id=$an[$dec].$id;
return $id;
}
echo alphanumeric(123456);
?>
VN:F [1.3.1_645]
Rating: 8.0/10 (17 votes cast)
Author: sunny Categories: Programing, Web Development Tags:
  1. No comments yet.
  1. No trackbacks yet.