是否有一个PHP只有电子邮件地址混淆功能? 大部分在网络上找到的那些都是JS和PHP的混合。
Answer 1:
我的最爱:
标记+ PHP
<span class="rev"><?php echo strrev($email); ?> </span>
CSS
.rev{
direction: rtl;
unicode-bidi: bidi-override;
}
小提琴
Answer 2:
这里有一对夫妇的功能我用。
第一个使用HTML字符代码混淆电子邮件地址:
function getObfuscatedEmailAddress($email)
{
$alwaysEncode = array('.', ':', '@');
$result = '';
// Encode string using oct and hex character codes
for ($i = 0; $i < strlen($email); $i++) {
// Encode 25% of characters including several that always should be encoded
if (in_array($email[$i], $alwaysEncode) || mt_rand(1, 100) < 25) {
if (mt_rand(0, 1)) {
$result .= '&#' . ord($email[$i]) . ';';
} else {
$result .= '&#x' . dechex(ord($email[$i])) . ';';
}
} else {
$result .= $email[$i];
}
}
return $result;
}
例:
echo getObfuscatedEmailAddress('firstname.last-name@example.com');
-->
firstname.last-name@example.com
第二个将返回链接,电子邮件地址是HTML和URL编码:
function getObfuscatedEmailLink($email, $params = array())
{
if (!is_array($params)) {
$params = array();
}
// Tell search engines to ignore obfuscated uri
if (!isset($params['rel'])) {
$params['rel'] = 'nofollow';
}
$neverEncode = array('.', '@', '+'); // Don't encode those as not fully supported by IE & Chrome
$urlEncodedEmail = '';
for ($i = 0; $i < strlen($email); $i++) {
// Encode 25% of characters
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;
}
例:
echo getObfuscatedEmailLink('firstname.last-name@example.com');
-->
<a href="mailto:%66i%72stna%6de.%6c%61st-name@example.com" rel="nofollow">firstname.last-name@example.com</a>
Answer 3:
这里有一个用PHP 7型提示。
只是把它叫做$this->obfuscateEmail($email);
/**
* @param string $email
* @return string
*/
private function obfuscateEmail(string $email) : string
{
$em = explode("@", $email);
$name = implode(array_slice($em, 0, count($em) - 1), '@');
$len = floor(strlen($name) / 2);
return substr($name, 0, $len) . str_repeat('*', $len) . "@" . end($em);
}
Answer 4:
在这里,在情况下的另一种方式存在主机前一个字符:
/**
* @param string $email
* @return string
*/
private function obfuscateEmail($email)
{
$em = explode("@", $email);
$name = implode(array_slice($em, 0, count($em)-1), '@');
if(strlen($name)==1){
return '*'.'@'.end($em);
}
$len = floor(strlen($name)/2);
return substr($name,0, $len) . str_repeat('*', $len) . "@" . end($em);
}
Answer 5:
这里是一个颇为紧张和不导致错误的另一种方式https://validator.w3.org
function obfuscate($email){
$encoded_email = '';
for ($a = 0,$b = strlen($email);$a < $b;$a++)
{
$encoded_email .= '&#'.(mt_rand(0,1) == 0 ? 'x'.dechex(ord($email[$a])) : ord($email[$a])) . ';';
}
return $encoded_email;}
然后添加到页面使用:
<a href="mailto:<?= obfuscate(CONTACT_EMAIL.'?subject=Hello!') ?>"><?= CONTACT_EMAIL ?></a>
文章来源: Looking for a php only email address obfuscator function