php正则入门,实习email和URL验证

匹配email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*

匹配网址url的正则表达式:[a-za-z]+://[^s]*

下面看一实例,代码如下:

  1. <body onload="f.a.select();">
  2. <?php
  3. echo 'a:'.htmlspecialchars($a);
  4. echo '<br>a1:';
  5. $a1 = eregi_replace('(^[_.][:alnum:]-]+@([[:alnum:]][[:alnum:]-]*.)+[[:alpha:]]{2,3}$)',' <a href="mailto:1">1</a> ', $a) ;
  6. echo htmlspecialchars($a1);
  7. echo "<br>a1:$a1";
  8. echo '<br>a2:';
  9. $a2 = eregi_replace('([[:alnum:]]+://([[:alnum:]][[:alnum:]-]*.)+[[:alpha:]]{2,3}([#?/][^#?/][:space:]][^[:space:]]*)?)',
  10.    '<a href="1" target="_blank">1</a>', $a);
  11. echo htmlspecialchars($a2);
  12. echo "<br>a2:$a2";
  13. // eregi('^[_.][:alnum:]-]+@([[:alnum:]][[:alnum:]-]*.)+[[:alpha:]]{2,3}$',$a) or die("not a email");
  14. ?>
  15. <form name="f">
  16. <input name="a" value="<?php echo $a; ?>">
  17. <input type="submit" >
  18. </form>
  19. </body>