PHP encodes email address

I need a PHP way to encode an email address using a-zA-Z0-9 , so it’s basically encoded without any special characters, but then you can decode it back to the original.

Example:

 john+doe@mydomain.com => ENCODE => n6bvJjdh7w6QbdVB373483ydbKus7Qx n6bvJjdh7w6QbdVB373483ydbKus7Qx => DECODE => john+doe@mydomain.com 

Is it possible?

+4
source share
2 answers

You can make base64 web safe code:

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

It converts + and / from base64 alphabet to safer - and _ . The encoding step also removes trailing = characters if necessary.

+7
source

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

Another option is bin2hex() and hex2bin()

+1
source

All Articles