English 中文(简体)
如何检查在扼杀中只有一个“@”特性? [复制]
原标题:How to check that there is only one "@" character in the string? [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Modify regex to validate email?

$email = $_POST["email"];

if(preg_match("[@{1}]",$email))
    echo "There is only one @ symbol";

if(preg_match("[@{2,}]",$email))
    echo "There is more than one";

这个问题很简单,但由于我很少使用定期表述,产出没有显示出我所希望的方式。 电子邮件也是员额数据。

如果电子邮箱有2个或2个以上@符号,就会显示有不止一个。 如果电子邮箱有1 @symbol,就会显示只有1 @文号。 更方便的权利?

最佳回答

您的首次发言将贴上任何地方的“@;从来说,这不需要是唯一的。

您的第二次发言将贴上两个或两个以上<>执行的<代码>@。 如果你有两个人与他人分离,就不会发现这种情况。

你们需要将“只有一个”或“不止一个”的概念转化为与规范相符的条款:

  • “只有一种”:单一<代码>@

  • “不止一个”:2个<代码>@与任何事项分开:@>*@

以及“任何但只有一个”的相关有用概念(即:0、2、3、4.)简单地作为否定第一个概念(即:!preg_match( /^[^@]*$/ , 电子邮箱:)

问题回答

我建议使用<代码>explode和count。 同样:

if (count(explode( @ , $email)) > 2) {
    //here you have 2 or more
}

你们想要达到的目标是什么? 您是否真的想要知道其中只有一个@,或者你是否想确认整个电子邮件地址? 如果你要证实,请看这个职位:。 修改条例以验证电子邮件:

you need to enclose your regex in delimiters like forward slash(/) or any other char.

$email = $_POST["email"];

if(preg_match("/[@{1}]/",$email))
    echo "There is only one @ symbol"."</br>";

//you have to use preg_match_all to match all chars because preg_match will stop at first occurence of match.

if(preg_match_all("/(w*@)/",$email,$matches)){             //w matches all alphanumeric chars, * means 0 or more occurence of preceeding char 
    echo "There is more than one"."</br>";
    print_r($matches);}                                 //$matches will be the array of matches found.
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签