Home  >  Article  >  Backend Development  >  Example of verifying email address with php regular expression

Example of verifying email address with php regular expression

WBOY
WBOYOriginal
2016-07-25 08:56:381093browse
This article introduces an example of using regular expressions to verify email addresses in PHP. Friends in need can refer to it.

An example of php implementing email address verification, which uses php regular rules very well.

1, html part

<HTML>
<BODY>
<H2>php正则验证Email地址</H2>
<FORM METHOD="POST" ACTION="RegularExpressionTester.php">
<FONT FACE="Courier">
String:
<BR>
<INPUT TYPE="TEXT" NAME="string">
<BR><BR>
正则表达式:
<BR>
<INPUT TYPE="TEXT" SIZE=64 NAME="pattern" 
       VALUE="^([_a-zA-Z0-9-]+)@([a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*)$">
<BR><BR>
<BR>
<INPUT TYPE="SUBMIT">
</FONT>
</FORM>
</BODY>
</HTML>

2, php code part RegularExpressionTester.php

<?php
echo "<BR><B>string:</B>$string";
echo "<BR><B>regular expression:</B> $pattern";
if (get_magic_quotes_gpc()){
    echo "<BR><BR>";
    echo "<BR>Stripping magic quotes....";
    $string = stripslashes($string);
    $pattern = stripslashes($pattern);
    echo "<BR><B>string:</B> $string";
    echo "<BR><B>regular expression:</B> $pattern";
}
$found = ereg($pattern, $string, $matches);
echo "<BR><BR>";
if ($found)
{
    echo "<BR><B>验证:</B> 成功";
    echo "<BR><BR>";
    echo "<BR><B>Components:&nbsp</B>";
    for ($i = 0; $i < count($matches); $i++)
    {
        echo "<BR>$matches[$i]";
    }
}
else
    echo "<BR><B>验证:</B> 失败";
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn