Viewed   55 times
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;
    }
}

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.

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

Thanks in advance!

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

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

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.

thanks everyone!

 Answers

3

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-dTH: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
# ...
Tuesday, November 1, 2022
5

Try something like this:

$text = '...{{aa{{bb}}cc}}...{{aa{{bb{{cc}}bb{{cc}}bb}}dd}}...';
preg_match_all('/{{(?:[^{}]|(?R))*}}/', $text, $matches);
print_r($matches);

output:

Array
(
    [0] => Array
        (
            [0] => {{aa{{bb}}cc}}
            [1] => {{aa{{bb{{cc}}bb{{cc}}bb}}dd}}
        )
)

And a short explanation:

{{      # match two opening brackets
(?:       # start non-capturing group 1
  [^{}]   #   match any character except '{' and '}'
  |       #   OR
  (?R)    #   recursively call the entire pattern: {{(?:[^{}]|(?R))*}}
)         # end non-capturing group 1
*         # repeat non-capturing group 1 zero or more times
}}        # match two closing brackets
Thursday, December 15, 2022
 
4

You are missing the /ims flag at the end of your regex. Otherwise . will not match line breaks (as in your first paragraph). Actually /s would suffice, but I'm always using all three for simplicity.

Also, preg_match works for many simple cases. But if you are attempting any more complex extractions, then consider alternating to phpQuery or QueryPath which allow for:

foreach (qp($html)->find("p") as $p)  { print $p->text(); }
Friday, September 23, 2022
 
1

Use preg_quote to quote regular expression characters.

Like this:

preg_quote($theKeyword, '/');

Where '/' is the delimiter in your regular expression.

Friday, November 25, 2022
 
3
preg_match('~"http://(.*)"~iU', $code, $matches);

Your issue was you need delimiters (I chose ~) to use with the pattern. See the preg_match() man page for more information.

Saturday, December 10, 2022
 
3

It seems like its a nightmare to fix the ?? display in the French locale, but I was able to fix this problem another way by modifying the regex pattern instead. By adding 'u' as a modifier in the patter it was able to detect the French character ç in ça and all works properly with no need to change the locale.

From this:

$pattern=(b".$value."b)

to this:

$pattern=(b".$value."b/u)
Tuesday, December 6, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :