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

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

Email正则表达式:

  1. $str_arr = array(
  2. "mymail@phpfensi.com",
  3. "my_mail@phpfensi.com",
  4. "my-mail@phpfensi.com",
  5. "my.mail@site.com.cn",
  6. "mymail@site.com.com",
  7. "mymail@site.cn",
  8. "mymail@@@lsite.com",
  9. "mymail@site",
  10. "mymail@phpfensi.com",
  11. "my2007@phpfensi.com",
  12. "163mail_for-me777@phpfensi.com",
  13. );
  14. $patt_email = "/^[_a-za-z0-9-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/";
  15. foreach ($str_arr as $str)
  16. {
  17. echo "字符串'$str':是";
  18. if (preg_match($patt_email, $str))
  19. {
  20. echo "合法的email格式";
  21. echo "

    ";

  22. echo "

    ";

  23. }
  24. else
  25. {
  26. echo "不合法的email格式";
  27. echo "

    ";

  28. echo "

    ";

  29. }
  30. }

url正则表达式:

  1. $str_arr = array(
  2. "http://www.phpfensi.com",
  3. "www.phpfensi.com",
  4. "http://www.phpfensi.com/abc/123.html",
  5. "//phpfensi.com",
  6. ":www.phpfensi.com"
  7. );
  8. $patt_url = "/^(http://)?[a-za-z0-9]+(.[a-za-z0-9]+)*.+$/";
  9. foreach ($str_arr as $str)
  10. {
  11. echo "字符串'$str':是";
  12. if (preg_match($patt_url, $str))
  13. {
  14. echo "合法的url格式";
  15. echo "

    ";

  16. echo "

    ";

  17. }
  18. else
  19. {
  20. echo "不合法的url格式";
  21. echo "

    ";

  22. echo "

    ";

  23. }
  24. }