tags:

views:

62

answers:

6

Is there a encoding function in PHP which will encode strings and the resulting output will only contain letters and numbers? I would use base64 but that still has some stuff which is not numeric/alphanumeric

+1  A: 

Any of the hash functions (md5, sha1, etc.) output will only consist of hexadecimal digits but that's not exactly 'encoding'.

Nev Stokes
Don't want to hash because it needs to be recoverable.
cappuccino
I know it's an additional step but can't you hash the string again when you need to recover the key for the APC cache?
Nev Stokes
Why does an APC key need to be recoverable? It's a cache, fetching with a one-way encoding seems pretty OK to me?
Wrikken
A: 

You can use the basic md5 hash function which output only alphanumeric characters.

gulbrandr
Yeah, and its counterpart `md5_decode()` ;) Seriously though, it looks like the OP is looking for an encode / decode pair, not a hashing function
Pekka
that's not an answer, if you don't like to invest the time for a real answer, don't post. This is SO, not some kind of forum.
tharkun
@tharkun: sorry about that. I thought it was enough as the md5 documentation is clear. I edited my answer.
gulbrandr
I removed my downvote.
tharkun
A: 

It really depends on your goal. Please explain in more detail what you want to achieve!

If you mean encoding in the sense of charset, then I doubt there is a charset that contains only alphanumeric members. base64 stands for a set of encodings limited to 64 chars, which is more than just the alphanumeric chars.

If you want to get rid of everything non-alphanumeric, I would suggest validating/filtering the content. You can use existing filter mechanism like in Zend_Validate or you can write your own filter. You can use Regular Expressions for this.

tharkun
Its to be used as the key to my APC cache objects, APC key's can't contain things other than a-z, 0-9 and i think underscores.
cappuccino
+3  A: 

You could use base32 (code easy to google), which is sort of a standard alternative to base64. Or resort to bin2hex() and pack("H*",$hex) to reverse. Hex encoding however leads to size doubling.

mario
(but if its of any significant size, and starting off as text, you can compress it first!)
symcbean
thank you, a straight answer!
cappuccino
A: 

You could write your own base-62 encoder/decoder using a-z/A-Z/0-9. You'd need 3 digits for every ASCII character though, so not that efficient.

Mike C
Why would he need 3 digits? Surely 2 would suffice.
Hammerite
I'm getting myself confused on this, but I think you're right.
Mike C
+1  A: 

Short answer is no, base64 uses a reduced set of output chars compared with uuencode and was intended to solve most character converions issues - but still isn't url-safe (IIRC).

But the machanism is trivial and easily adapted - I'd suggest having a look at base32 encoding - same as base64 but using one less bit per input char to create the output (and hence a 32 char alphabet is all that's required) but using something different for the padding char ('=' is not url safe).

A quick google found this

symcbean