Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP encode e-mail address

Tags:

php

encode

hash

I need a way in PHP to encode an e-mail address only using a-zA-Z0-9 so basically encoded without any special characters, but then be able to decode it back to the original.

Example:

 [email protected] => ENCODE => n6bvJjdh7w6QbdVB373483ydbKus7Qx
 n6bvJjdh7w6QbdVB373483ydbKus7Qx => DECODE => [email protected]

Is this even possible?

like image 304
Justin Avatar asked Feb 19 '23 00:02

Justin


2 Answers

You can do a web safe base64 encode:

// encode
$email_encoded = rtrim(strtr(base64_encode($email), '+/', '-_'), '=');

// decode
$email_decoded = base64_decode(strtr($email_encoded, '-_', '+/'));

It converts the + and / from the base64 alphabet in the more harmless - and _. The encoding step also removes the trailing = characters when needed.

like image 141
Ja͢ck Avatar answered Feb 28 '23 00:02

Ja͢ck


If you allow the equal sign, you can use base64_encode() and base64_decode()

Another option is bin2hex() and hex2bin()

like image 44
sachleen Avatar answered Feb 27 '23 23:02

sachleen