Home  >  Article  >  php教程  >  php 日期正则表达式

php 日期正则表达式

WBOY
WBOYOriginal
2016-06-08 17:26:181386browse

本文章提供一款利用正则表达式来验证日期是不是正则的日期格式了,第一个是一款正常的日期验证,安全通过了。第二款日期格式有问题,所以输出日期不合法了。

<script>ec(2);</script>
 代码如下 复制代码
      $date="2003-01-31";
      if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
             echo "$regs[3] / $regs[2] / $regs[1]

";
             echo "$regs[0]

";
             echo "$regs[1]年 $regs[2]月 $regs[3]日";
      } else {
            echo "不合法的日期格式:$date";
       }

//日期实例二

 代码如下 复制代码
      $date="2020.01-31";
      if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
             echo "$regs[3] / $regs[2] / $regs[1]

";
             echo "$regs[0]

";
             echo "$regs[1]年 $regs[2]月 $regs[3]日";
      } else {
            echo "不合法的日期格式:$date";
       }


?>

Statement:
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