验证邮箱格式 用户名 url 的php正则表达式

用到了php的ereg函数来验证电子邮箱地址,用户名,url地址实例.

把url拆分,代码如下:

  1. <?
  2. $url = "http://www.phpfensi.com";
  3. $www_url = ereg("^(http://www).([[:alnum:]]+).([[:alnum:]]+)", $url, $regs);
  4. if ($www_url) :
  5. echo $regs[0];
  6. print "<br>";
  7. echo $regs[1];
  8. print "<br>";
  9. echo $regs[2];
  10. print "<br>";
  11. echo $regs[3];
  12. endif;
  13. ?>

验证邮箱地址,代码如下:

  1. <?php
  2. $s = 'm@t.ca';
  3. if (ereg ('([[:alpha:]]+)@([[:alpha:]]+).([[:alpha:]]{2,4})', $s, $matches))
  4. {
  5. echo "Regular expression successful. Dumping matchesn";
  6. var_dump ($matches);
  7. }
  8. else
  9. {
  10. echo "Regular expression unsuccessful.n";
  11. }
  12. ?>

验证邮箱地址实例二,代码如下:

  1. <?php
  2. $email="a@a.com";
  3. $is_com = ereg("(.)(com$)", $email);
  4. print $is_com;
  5. ?>

用户名验证,代码如下:

  1. <?php
  2. $username = "Test";
  3. if (ereg("([^a-z])",$username)) echo "Username must be all lowercase!";
  4. ?>