How to safely encode PHP string into alphanumeric only?

hex2bin($string);

pack("H*" , $hex_str);