Have you ever been faced with a situation where you need to encode a string with all sorts of problem characters into a URL-safe string, but, for whatever reason, urlencode() just won't do the job?
What you need is a function that will give you nothing but numbers and letters-- no %'s or &'s, spaces, or any other non-alphanumeric character.
Well, look no further that PHP's bin2hex() function, and its pack() function.
It can convert this tricky string:
(!X>4Ob=h/&hN\'
Into this much nicer string, which can easily pass through a URL, MySQL query, XML tags, etc, since it is guaranteed to only ever be letters and numbers:
2821583e344f623d682f26684e5c27
And then decoding is a snap. Here are the functions you need:
function hex_encode($input) {
return bin2hex($input);
}
function hex_decode($input) {
return pack("H*", $input);
}
Enoy!
#1: Size matters
Most of the time this problem presents itself, the web developer believes he or she is going insane. Your site looks beautiful in FireFox and Chrome, but in IE everything is off: some things are positioned weird, fonts are the wrong size, etc. And IE doesn't warn you or let you know this is happening in any way, so you are left to go silently mad trying to figure out what the problem is.