0

Does anyone knows why i receive this error : preg_match() [function.preg-match]: Unknown modifier '(' using this method:

function checkFBDateFormat($date) {
    if(preg_match ("/^([0-9]{2})/([0-9]{2})/([0-9]{4})$/", $date, $parts)){
        if(checkdate($parts[2],$parts[1],$parts[3]))
            return true;
        else
            return false;
    } else {
        return false;
    }
}
KingCrunch
  • 128,817
  • 21
  • 151
  • 173

6 Answers6

0

You use / as delimiter for your expression. However, it's completely unnecessary anyway

$parts = explode('/', $date);

Even better: http://php.net/datetime.createfromformat

To give you an idea what happens: PCRE regular expression require a delimiter at the start and the end of the pattern itself. Everything after the second delimiter is treated as modifier. Thus you decided to use / as delimiter (it's always the first character), so your pattern ended right after /^([0-9]{2})/. Everything next (which is a ( at first) is treated as modifier, but ( is not an existing modifier.

If you want to stay with regular expression, I recommend to use another delimiter like

~^([0-9]{2})/([0-9]{2})/([0-9]{4})$~
#^([0-9]{2})/([0-9]{2})/([0-9]{4})$#

Just read the manual about the PCRE-extension

Two additional comments:

  • You should define $parts, before you use it
  • Remember, that the expression is quite inaccurate, because it allows dates like 33/44/5678, but denies 1/1/1970
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
0

If the first char is e.g. an slash / is detected as delimiter fro the regular expression. Thus your regex is only the part ^([0-9]{2}). And everything after the second slash is recognized as modifiers for the regex.

If you really want to match a slash, use \/ to escape it

feeela
  • 29,399
  • 7
  • 59
  • 71
0

Since you are using slash in regular expression, need use other delimiter, try:

preg_match ("#^([0-9]{2})/([0-9]{2})/([0-9]{4})$#", $date, $parts)
Paulo Rodrigues
  • 5,273
  • 7
  • 35
  • 58
0

You need to escape your slash, like so:

if(preg_match ("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $date, $parts)){
Nelson
  • 49,283
  • 8
  • 68
  • 81
0

You did not escape your "/" and you didn't complete your if statements either, please try this:

        function checkFBDateFormat($date) {
        if(preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $date, $parts)){
            if(checkdate($parts[2],$parts[1],$parts[3])) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

echo var_dump(checkFBDateFormat('08/09/2012'));
Asciiom
  • 9,867
  • 7
  • 38
  • 57
mzimhle
  • 72
  • 1
  • 10
0

You might want to consider not using regular expressions at all.

<?php
// simple example
$timestamp = strtotime('12/30/2012');
if ($timestamp) {
    // valid date… Now do some magic
    echo date('r', $timestamp);
}
Jan.
  • 1,925
  • 15
  • 17