PHP函数preg_replace不起作用

时间:2014-08-21 11:47:07

标签: php preg-replace

我正在检查文本是否包含电子邮件模式,但它不起作用,我不明白为什么。 这是我的代码:

$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
$replacement = "**************";
$_POST['description'] = preg_replace($pattern, $replacement, $_POST['description']);

2 个答案:

答案 0 :(得分:1)

如何移除锚点:

$pattern = "/\b[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,5}\b/";

答案 1 :(得分:0)

如果你不想要preg_replace,你可以使用我在网上找到的这个功能

function isValidEmail($address) {
    if (filter_var($address,FILTER_VALIDATE_EMAIL)==FALSE) {
        return false;
    }
    /* explode out local and domain */
    list($local,$domain)=explode('@',$address);

    $localLength=strlen($local);
    $domainLength=strlen($domain);

    return (
        /* check for proper lengths */
        ($localLength>0 && $localLength<65) &&
        ($domainLength>3 && $domainLength<256) &&
        (
            checkdnsrr($domain,'MX') ||
            checkdnsrr($domain,'A')
        )
    );
}

Ref

相关问题