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!