寻找一个php唯一的电子邮件地址混淆功能

tkqqtvp1  于 2023-01-04  发布在  PHP
关注(0)|答案(6)|浏览(106)

有没有只支持php的邮件地址混淆功能?大多数在网上找到的都是JS和PHP的混合体。

bqf10yzr

bqf10yzr1#

下面是我使用的几个函数。
第一个使用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');
-->
firstn&#x61;m&#x65;&#x2e;la&#115;t-name&#x40;examp&#108;e&#46;&#x63;om

第二个将返回链接,其中电子邮件地址是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="mailt&#111;&#58;%66i&#37;72stna%&#54;d&#x65;&#46;&#37;6c&#x25;6&#x31;st&#x2d;name&#64;&#101;&#x78;&#x61;mple&#46;co&#109;" rel="nofollow">f&#x69;&#114;s&#x74;na&#109;e&#x2e;&#108;a&#x73;t-name&#64;e&#x78;ample&#46;co&#109;</a>
avwztpqn

avwztpqn2#

我的最爱:

标记+ PHP

<span class="rev"><?php echo strrev($email); ?> </span>

中央支助组

.rev{
    direction: rtl;
    unicode-bidi: bidi-override;
}

Fiddle

k5hmc34c

k5hmc34c3#

下面是另一种方法,它非常紧凑,不会导致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>
cld4siwp

cld4siwp4#

下面是一个使用PHP7类型提示的示例。
$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);
}
jogvjijk

jogvjijk5#

这里有一个替代方法,如果有一个字符之前的主机:

/**
* @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);
}
3bygqnnd

3bygqnnd6#

这里列出的一些例子很棒,但我使用了WordPress的antispambot()函数,为了方便起见,如下所示:

function antispambot( $email_address, $hex_encoding = 0 ) {
    $email_no_spam_address = '';
    for ( $i = 0, $len = strlen( $email_address ); $i < $len; $i++ ) {
        $j = rand( 0, 1 + $hex_encoding );
        if ( 0 == $j ) {
            $email_no_spam_address .= '&#' . ord( $email_address[ $i ] ) . ';';
        } elseif ( 1 == $j ) {
            $email_no_spam_address .= $email_address[ $i ];
        } elseif ( 2 == $j ) {
            $email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[ $i ] ) ), 2 );
        }
    }

    return str_replace( '@', '&#64;', $email_no_spam_address );
}

function zeroise( $number, $threshold ) {
    return sprintf( '%0' . $threshold . 's', $number );
}

有关详细信息,请参见WordPress

相关问题