这里提供两种关于Email正则表达式与URL正则表达式验证的正则,非常的实用,关于email的正则表达式可以把内容中的所有邮箱地址获取出来,而url地址正则也一样,可以把内容中所有http开头url地址保存提取来哦。
Email正则表达式:
- $str_arr = array(
- "mymail@phpfensi.com",
- "my_mail@phpfensi.com",
- "my-mail@phpfensi.com",
- "my.mail@site.com.cn",
- "mymail@site.com.com",
- "mymail@site.cn",
- "mymail@@@lsite.com",
- "mymail@site",
- "mymail@phpfensi.com",
- "my2007@phpfensi.com",
- "163mail_for-me777@phpfensi.com",
- );
- $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 "
";
- }
- }
url正则表达式:
- $str_arr = array(
- "http://www.phpfensi.com",
- "www.phpfensi.com",
- "http://www.phpfensi.com/abc/123.html",
- "//phpfensi.com",
- ":www.phpfensi.com"
- );
- $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 "
";
- }
- }
|