Home > php教程 > php手册 > Email正则表达式与URL正则表达式

Email正则表达式与URL正则表达式

WBOY
Release: 2016-06-13 10:03:16
Original
1497 people have browsed it

Email正则表达式与URL正则表达式 这里提供两种关于Email正则表达式与URL正则表达式验证的正则,非常的实用,关于email的正则表达式可以把内容中的所有邮箱地址获取出来,而url地址正则也一样,可以把内容中所有http开头url地址保存提取来哦。

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 "
";
    }
}

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template