How to safely encode PHP string into alphanumeric only string?
E.g. “Hey123 & 5” could become “ed9e0333” or may be something better looking
It’s not about stripping characters, its about encoding.
The goal is to make any string after this encoding suitable for css id string (alnum),
but later I will need to decode it back and get the original string.
bin2hexseems to fit the bill (although not as compact as some other encodings). Also take care that CSS ids cannot start with a number, so to be sure you’ll need to prefix something to thebin2hexresult before you have your final ID.For the reverse (decoding), there’s no such thing as
hex2bin, but someone on the PHP documentation site suggested this (untested):