Home  >  Article  >  Backend Development  >  Email Regular Expressions and URL Regular Expressions_PHP Tutorial

Email Regular Expressions and URL Regular Expressions_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:58:571412browse

Email regular expression and URL regular expression Here are two regular expressions for email regular expression and URL regular expression verification, which are very practical. The regular expression for email can get all the email addresses in the content, and the same is true for URL address regular expressions. You can get all the email addresses in the content. Save and extract all URL addresses starting with http.

email regular expression and url regular expression
Here are two regular expressions for email regular expression and url regular expression verification, which are very practical. The regular expression for email can get all the email addresses in the content, and the same goes for the url address regular expression, which can be used to get all the email addresses in the content. Save and extract all URL addresses starting with http.
*/

$str_arr = array(
"mymail@bKjia.c0m",
"my_mail@bKjia.c0m",
"my-mail@bKjia.c0m",
"my.mail@site.com.cn",
"mymail@site.com.ccoomm",
"mymail@site.cn",
"mymail@@@lsite.com",
"mymail@site",
"mymail@bKjia.c0m",
"my2007@bKjia.c0m",
"163mail_for-me777@bKjia.c0m",
);

$patt_email = "/^[_a-za-z0-9-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$ /";

foreach ($str_arr as $str)
{
echo "String '$str': Yes";
If (preg_match($patt_email, $str))
{
echo "Legitimate email format";
echo "
";
echo "
";
}
else
{
echo "Illegal email format";
echo "
";
echo "
";
}
}

// 17. URL regular expression.

$str_arr = array(
"http://www.bKjia.c0m",
"www.bKjia.c0m",
"http://www.bKjia.c0m/abc/123.html",
"//bKjia.c0m",
":www.bKjia.c0m"
);

$patt_url = "/^(http://)?[a-za-z0-9]+(.[a-za-z0-9]+)*.+$/";

foreach ($str_arr as $str)
{
echo "String '$str': Yes";
If (preg_match($patt_url, $str))
{
echo "Legal URL format";
echo "
";
echo "
";
}
else
{
echo "Illegal url format";
echo "
";
echo "
";
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631362.htmlTechArticleEmail regular expression and URL regular expression Here are two types of verification of Email regular expression and URL regular expression Regular expressions are very practical. Regarding email regular expressions, you can convert the content...
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