obfuscate email address php code example
Example 1: php obfuscate email
function getObfuscatedEmailLink($email, $params = array())
{
if (!is_array($params)) {
$params = array();
}
if (!isset($params['rel'])) {
$params['rel'] = 'nofollow';
}
$neverEncode = array('.', '@', '+');
$urlEncodedEmail = '';
for ($i = 0; $i < strlen($email); $i++) {
if (!in_array($email[$i], $neverEncode) && mt_rand(1, 100) < 25) {
$charCode = ord($email[$i]);
$urlEncodedEmail .= '%';
$urlEncodedEmail .= dechex(($charCode >> 4) & 0xF);
$urlEncodedEmail .= dechex($charCode & 0xF);
} else {
$urlEncodedEmail .= $email[$i];
}
}
$obfuscatedEmail = getObfuscatedEmailAddress($email);
$obfuscatedEmailUrl = getObfuscatedEmailAddress('mailto:' . $urlEncodedEmail);
$link = '<a href="' . $obfuscatedEmailUrl . '"';
foreach ($params as $param => $value) {
$link .= ' ' . $param . '="' . htmlspecialchars($value). '"';
}
$link .= '>' . $obfuscatedEmail . '</a>';
return $link;
}
Example 2: php obfuscate email
echo getObfuscatedEmailLink('[email protected]');
-->
<a href="mailto:%66i%72stna%6de.%6c%[email protected]" rel="nofollow">firstname.last-name@example.com</a>