Welcome to WebmasterWorld Guest from 54.198.87.238

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

compressing simple numbers

     
8:35 am on Sep 28, 2010 (gmt 0)

5+ Year Member



Let's say I have a number between 100,000 and 999,999 (1 less than a million, that is)

What would be the cleanest way to 'compress' them into something smaller?

I only want to have A-Z/a-z or even A-Z/a-z/0-9

I'm thinking of changing character set to a lower size, but how do I do that in php?

The result of compressing '234567' would give something like 'AfE4'
Decompressing 'AfE4' would give me back '234567'
9:57 am on Sep 28, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



You can use base_convert

$string = 123456;
$conv = base_convert($string, 10, 36);
$string = base_convert($conv, 36, 10);
echo $string;
10:00 am on Sep 29, 2010 (gmt 0)

5+ Year Member



I'm thinking of changing character set to a lower size, but how do I do that in php?


While not nearly as simple as using base_convert, I posted a "NumberEncoder" class example a while back which you may be interested in, or not. It is a bit lengthy (heavily commented), though offers ability to use character combinations which would not be easily decodable without knowing the original 'key' used to initialize the class.
You can find it here: class NumberEncoder [webmasterworld.com]
(be sure to read my follow-ups there, and replace the 'decode' method with my last entry there)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month