Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

regex - Email validation using regular expression in PHP

I am pretty much new with regular expression. I am developing a project in PHP and i need to validate email address. After searching in this site and google i found the following regular expression says it should work best.

if (preg_match("/^(?!(?:(?:x22?x5C[x00-x7E]x22?)|(?:x22?[^x5Cx22]x22?)){255,})(?!(?:(?:x22?x5C[x00-x7E]x22?)|(?:x22?[^x5Cx22]x22?)){65,}@)(?:(?:[x21x23-x27x2Ax2Bx2Dx2F-x39x3Dx3Fx5E-x7E]+)|(?:x22(?:[x01-x08x0Bx0Cx0E-x1Fx21x23-x5Bx5D-x7F]|(?:x5C[x00-x7F]))*x22))(?:.(?:(?:[x21x23-x27x2Ax2Bx2Dx2F-x39x3Dx3Fx5E-x7E]+)|(?:x22(?:[x01-x08x0Bx0Cx0E-x1Fx21x23-x5Bx5D-x7F]|(?:x5C[x00-x7F]))*x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))]))$/iD", strtolower($mail_address))) {
    echo "valid email";
}

But when I use it I get error says,

"Warning: preg_match(): No ending delimiter '/' found"

What is wrong with this code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this instead of a regular expression:

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
     //Valid email!
}

Using regular expressions to validate email addresses is not recommended as it is certainly not pretty, especially if you don't want to exclude somebody who has a valid email address that is correct according to RFC 822 grammar.

http://www.php.net/filter_var is your best bet.

A crazy example of a regex that attempts to validate email addresses according to RFC 822 grammar:

(?:(?:
)?[ ])*(?:(?:(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ]
)+||(?=[["()<>@,;:".[]]))|"(?:[^"
\]|\.|(?:(?:
)?[ ]))*"(?:(?:

)?[ ])*)(?:.(?:(?:
)?[ ])*(?:[^()<>@,;:".[] 00-31]+(?:(?:(
?:
)?[ ])+||(?=[["()<>@,;:".[]]))|"(?:[^"
\]|\.|(?:(?:
)?[ 
]))*"(?:(?:
)?[ ])*))*@(?:(?:
)?[ ])*(?:[^()<>@,;:".[] 00-
31]+(?:(?:(?:
)?[ ])+||(?=[["()<>@,;:".[]]))|[([^[]
\]|\.)*
](?:(?:
)?[ ])*)(?:.(?:(?:
)?[ ])*(?:[^()<>@,;:".[] 00-31]+
(?:(?:(?:
)?[ ])+||(?=[["()<>@,;:".[]]))|[([^[]
\]|\.)*](?:
(?:
)?[ ])*))*|(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ])+|
|(?=[["()<>@,;:".[]]))|"(?:[^"
\]|\.|(?:(?:
)?[ ]))*"(?:(?:
)
?[ ])*)*<(?:(?:
)?[ ])*(?:@(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
r
)?[ ])+||(?=[["()<>@,;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<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ])+||(?=[["()<>
@,;:".[]]))|[([^[]
\]|\.)*](?:(?:
)?[ ])*))*(?:,@(?:(?:
)?[
 ])*(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ])+||(?=[["()<>@,
;:".[]]))|[([^[]
\]|\.)*](?:(?:
)?[ ])*)(?:.(?:(?:
)?[ ]
)*(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ])+||(?=[["()<>@,;:\
".[]]))|[([^[]
\]|\.)*](?:(?:
)?[ ])*))*)*:(?:(?:
)?[ ])*)?
(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ])+||(?=[["()<>@,;:".
[]]))|"(?:[^"
\]|\.|(?:(?:
)?[ ]))*"(?:(?:
)?[ ])*)(?:.(?:(?:

)?[ ])*(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ])+||(?=[[
"()<>@,;:".[]]))|"(?:[^"
\]|\.|(?:(?:
)?[ ]))*"(?:(?:
)?[ ])
*))*@(?:(?:
)?[ ])*(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ])
+||(?=[["()<>@,;:".[]]))|[([^[]
\]|\.)*](?:(?:
)?[ ])*)(?:
.(?:(?:
)?[ ])*(?:[^()<>@,;:".[] 00-31]+(?:(?:(?:
)?[ ])+|
|(?=[["()<>@,;:".[]]))|[([^[]
\]|\.)*](?:(?:
)?[ ])*))*>(?:(
?:
)?[ ])*))*)?;s*)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...