Obfuscate email address php

A classic request!

There are several ways to obfuscate an email address in PHP, which means making it difficult for spammers and bots to harvest the address. Here are a few methods:

Method 1: Using JavaScript

You can use JavaScript to obfuscate the email address. This method is client-side, so it won't protect the email address from being harvested by bots or spiders.

<?php
$email = '[email protected]';
?>
<script>
  var email = '<?php echo $email;?>';
  email = email.replace('@', '@x');
  email = email.replace('.', '.x');
  document.write(email);
</script>

Method 2: Using PHP

You can use PHP to obfuscate the email address. This method is server-side, so it will protect the email address from being harvested by bots or spiders.

<?php
$email = '[email protected]';
$email = str_replace('@', '@x', $email);
$email = str_replace('.', '.x', $email);
echo $email;
?>

Method 3: Using a library

You can use a PHP library like obfuscate-email to obfuscate the email address.

<?php
require 'obfuscate-email/autoload.php';

$email = '[email protected]';
$obfuscator = new \ObfuscateEmail\Obfuscator();
$email = $obfuscator->obfuscate($email);
echo $email;
?>

Method 4: Using a simple encoding

You can use a simple encoding method like Base64 to obfuscate the email address.

<?php
$email = '[email protected]';
$email = base64_encode($email);
echo $email;
?>

Remember that while these methods can make it more difficult for spammers and bots to harvest your email address, they are not foolproof. It's still important to use other security measures, such as using a contact form instead of displaying your email address directly.