I’m looking for a simple solution to reduce the length of my GET input code (on my website).
The forms are handled in JavaScript and, using a function, made simpler for URL sharing. For example: multiple form inputs, with different names, are converted into a simple settings=1,0,0,1 and placed in the URL as GET – this data is then exploded/imploded in PHP to use/re-use the input.
But, given the size of the form, it can still get pretty bloated. I’d like to compress that same data in the JavaScript and then decompress it in the PHP, allowing considerably shorter URLs for sharing.
Think TinyUrl. I did some googling, but solutions were more complex than I’d like – this isn’t an attempt to hide data, but simply make it shorter.
Update: A better example of what I want is to to convert a long GET input like: settings=1,1,1,1,1,1,1,1,1,1,1,1,1 into settings=s8djh.
You could create your own “short URLs” like this: Create a unique id from the parameters (for example a md5 hash) and store the id-parameter pair in the data store.
You can use a database, key-value-store like BerkeleyDB or a noSQL db or flat text file that stores two values.
If the MD5 hash is still too long, you have two options: 1) Just use a counter that is counted up for each parameter string you store. Note however, that each of the parameter combinations can then be reached by simple URL manipulation. The other option would be to create a random n-character string (n at least >= 4), look if it exists in the database. If it does not exist, use the string as the id. If it does exist, try 2-5 times with nerw strings and if they all exist, bump up n by one. As time passes, you will fill up the 4-char namespace, then the 5-char namespace, etc. The 6-char namespace (using upper- and lowercase and digits) allows for 62^6 combinations – a lot to fill up!
See this introductory article for some example code. It uses MD5 hashes encoded in base62 for the hashes, making them less than the usual 32 hexadecimal chars long.
Edit: If you don’t want to use a data store, maybe using an actual compression algorithm like LZW in JavaScript may shorten long rows of identical parameters.