Search code examples
phpencodeemail-address

PHP Encode an email address hide from spammers, decode easily without flaws?


What PHP function or encoding method could allow you to convert an email address to a set of characters, then decoded again when needed? The email address would be publicly available to a certain program/audience, which handles conversions, but would not be harvested by spammers due to not being recognized as an email address.

Obviously it would have to have a perfect 1 to 1 conversion.


Solution

  • You can do web safe encode and decode using this

    // encode emailaddress
    $email_encoded = rtrim(strtr(base64_encode($email), '+/', '-_'), '=');
    
     // decode email address
    $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.