4

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

cappuccino
  • 2,175
  • 5
  • 26
  • 48
  • 2
    What is it for ? I think it can be a specific answer. – MatTheCat Oct 29 '10 at 08:08
  • I asked a similar question some time ago: [Encoding arbitrary data into numbers?](http://stackoverflow.com/questions/2982112/encoding-arbitrary-data-into-numbers) I haven't had the time yet to test the answers, but @Artefacto's looks excellent – Pekka Oct 29 '10 at 08:08
  • Convert all characters to their ASCII codes using ord() ? – Mchl Oct 29 '10 at 08:15
  • It seems [`Alphabet::convert($str, Alphabet::BYTE, Alphabet::ALPHANUMERIC)`](https://github.com/delight-im/PHP-BaseConvert) may be what you want, i.e. the alphabet of base *62*. You could restrict this to just lowercase (`Alphabet::ALPHANUMERIC_LOWERCASE`) or uppercase (`Alphabet::ALPHANUMERIC_UPPERCASE`) as well, but that would produce longer outputs. – caw Aug 21 '19 at 13:18

6 Answers6

6

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
  • 144,265
  • 20
  • 237
  • 291
1

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
  • 47,736
  • 6
  • 59
  • 94
0

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

Nev Stokes
  • 9,051
  • 5
  • 42
  • 44
0

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
  • 1,808
  • 15
  • 17
0

I wrote this to use letters, numbers and dashes.

I'm sure you can improve it to take out the dashes:

function pj_code($str) {
    $len = strlen($str);
    while ($len--) {
        $enc .= base_convert(ord(substr($str,$len,1)),10,36) . '-';
    }
    return $enc;
}


function pj_decode($str) {
    $ords = explode('-',$str);
    $c = count($ords);
    while ($c--) {
        $dec .= chr(base_convert($ords[$c],36,10));
    }
    return $dec;
}
PJ Brunet
  • 3,615
  • 40
  • 37
-2

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

gulbrandr
  • 1,005
  • 1
  • 9
  • 16
  • 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 Oct 29 '10 at 08:09
  • 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. – markus Oct 29 '10 at 08:14
  • @tharkun: sorry about that. I thought it was enough as the md5 documentation is clear. I edited my answer. – gulbrandr Oct 29 '10 at 08:31