homepage Welcome to WebmasterWorld Guest from 54.161.247.22
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
compressing simple numbers
kajje




msg:4207953
 8:35 am on Sep 28, 2010 (gmt 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'

 

enigma1




msg:4208001
 9:57 am on Sep 28, 2010 (gmt 0)

You can use base_convert

$string = 123456;
$conv = base_convert($string, 10, 36);
$string = base_convert($conv, 36, 10);
echo $string;

astupidname




msg:4208623
 10:00 am on Sep 29, 2010 (gmt 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)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved