Welcome to WebmasterWorld Guest from 54.145.176.252

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

compressing simple numbers

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

New User

5+ Year Member

joined:Jan 8, 2010
posts: 18
votes: 0


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 Sept 28, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 0


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 Sept 29, 2010 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


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)