Email正则表达式与URL正则表达式_PHP教程
email正则表达式与url正则表达式
这里提供两种关于email正则表达式与url正则表达式验证的正则,非常的实用,关于email的正则表达式可以把内容中的所有邮箱地址获取出来,而url地址正则也一样,可以把内容中所有http开头url地址保存提取来哦。
*/
$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 "字符串'$str':是";
if (preg_match($patt_email, $str))
{
echo "合法的email格式";
echo "
";
echo "
";
}
else
{
echo "不合法的email格式";
echo "
";
echo "
";
}
}
// 17、url正则表达式。
$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 "字符串'$str':是";
if (preg_match($patt_url, $str))
{
echo "合法的url格式";
echo "
";
echo "
";
}
else
{
echo "不合法的url格式";
echo "
";
echo "
";
}
}