PHP preg_match与可使用的正则表达式 [英] PHP preg_match with working regex

查看:92
本文介绍了PHP preg_match与可使用的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function validateDate( $date )
{ 
    echo $date;
    //2012-08-24 20:30:00
    if(preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([1-2]{1})([0-9]{1}):([0-5]{1})([0-9]{1}):([0-5]{1})([0-9]{1})$/', $date) >= 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

这总是返回false.我使用了唯一的工具来构建正则表达式,并且在那儿工作得很好.当我在正则表达式中添加"/"时,麻烦开始了. PHP似乎以某种方式要求这些,但是我不知道为什么,我也不知道为什么它破坏了我的正则表达式.

This always returns false. I used an only tool to build the regular expression and it was working fine there. Trouble started when I added the "/" to the regex. Somehow PHP seems to require these but I dont know why and I dont know why it breaks my regex.

它应该为sth返回TRUE.例如"2012-08-24 20:30:00"和"asdf2012-08-24 20:30:00asdf"为FALSE或不符合我的正则表达式的任何内容

It should return TRUE for sth. like "2012-08-24 20:30:00" and FALSE for "asdf2012-08-24 20:30:00asdf" or anything thats not acording to my regex

提前谢谢!

-----------------------编辑 感谢您的所有回答!

-----------------------EDIT Thanks for all your answers!

正如一些用户指出的那样,我的函数在示例日期"2012-08-24 20:30:00"中返回true.但是,只有当我手动设置$ date ='2012-08-24 20:30:00'时,它才能执行该操作.如果我在代码的其他位置使用完全相同的字符串调用该函数,则返回false.有人知道为什么吗?

As some users pointed out my function returns true for the sample date "2012-08-24 20:30:00". However it does that only if i manually set $date='2012-08-24 20:30:00'. If i call the function elsewhere in my code with the exact same string it returns false. Does anyone know why?

----------------------- EDIT2

-----------------------EDIT2

很抱歉浪费您的时间,实际上是在字符串中添加了一些空格.在调用函数之前,在约会时使用trim()可以得出正确的结果.

Yea sorry for wasting your time, it was in fact some whitespace that was added to the string. using trim() on my date before calling my function gives the correct result now.

谢谢大家!

推荐答案

为什么使用正则表达式?使用 DateTime 类.

Why use regex? Use DateTime class.

function validateDate($date, $format = 'Y-m-d H:i:s')
{
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}

您可以将此功能用于各种日期/时间验证.例子:

You can use this function for all kind of date/time validations. Examples:

var_dump(validateDate('2012-02-28 12:12:12')); # true
var_dump(validateDate('2012-02-30 12:12:12')); # false
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false
var_dump(validateDate('14:50', 'H:i')); # true
var_dump(validateDate('14:77', 'H:i')); # false
var_dump(validateDate(14, 'H')); # true
var_dump(validateDate('14', 'H')); # true

var_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true
# or
var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # true

var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true
# or
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true
var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # false
# ...

这篇关于PHP preg_match与可使用的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆