Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Javascript reg表达式转换为PHP_Javascript_Php - Fatal编程技术网

将Javascript reg表达式转换为PHP

将Javascript reg表达式转换为PHP,javascript,php,Javascript,Php,我有以下Javascript正则表达式: function EMailRegularExpression(txtEMail) { var RegExpression = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/; if(txtEMail.toString().match(RegExpression)) return true; else return false; } 有人知道如何将其转换为PHP吗

我有以下Javascript正则表达式:

function EMailRegularExpression(txtEMail)
{
    var RegExpression = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/;

    if(txtEMail.toString().match(RegExpression)) return true;
    else return false;

}
有人知道如何将其转换为PHP吗?

您可以使用:

function some_function($email)
{
    if (preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/', $email)) {
        return true;
    } else {
        return false;
    }
}

或者正如@AndrewR所指出的:

function some_function($email)
{
    return preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/', $email);
}
您可以使用:

function some_function($email)
{
    if (preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/', $email)) {
        return true;
    } else {
        return false;
    }
}

或者正如@AndrewR所指出的:

function some_function($email)
{
    return preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/', $email);
}

看一看,我投票结束这个问题,因为用PHP编写正则表达式检查在整个网站和web上都有很好的文档记录。请尝试编写代码,如果在运行代码时遇到问题,请发布一个新问题。请看一看,我投票将此问题作为主题外的问题来结束,因为在本网站和web上,用PHP编写正则表达式检查都有很好的文档记录。请尝试编写代码,如果在运行时遇到问题,请发布新问题。这是一种反模式。
if
语句是多余的。当然,您可以简单地使用
返回preg\u match('/^[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/',$email)但对于某些用户来说,它可能很难阅读。这是一种反模式。
if
语句是多余的。当然,您可以简单地使用
返回preg\u match('/^[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/',$email)但对于某些用户来说,它可能很难阅读。