Reputation: 67
Im using preg_match_all to check dates and words that starts with capital, the problem is on the dates because on the regex tester its telling me that this regex is fine but in the php script its not doing it correctly, my pattern is this:
$pattern = "#(((0[1-9]|[12][0-9]|3[01])([\/\.\\\-])((0[1-9]|1[012])\11)?)(\d\d\d\d|\d\d))+|([A-Z][a-z]+)(\s[A-Z][a-z]+)*#";
and I want it to match this: "12.10.1990" as well as "12.10.90"
thanks for your help!
Upvotes: 1
Views: 1062
Reputation: 15045
$string = '12.10.1990 as well as 12.10.90';
preg_match_all('/[01]\d\.[0-3]\d\.\d{2,4}/', $string, $match);
print_r($match);
Use this pattern for the date matching portion of your regex. Regardless you are trying to re-invent the wheel. There are built in PHP functions that can help you better determine if a date is valid or not.
Use explode() and then put each segment into this function for example:
$string = '12.10.1990';
//$string = '12.10.90';
$string = explode('.', $string);
var_dump(checkdate($string[0], $string[1], $string[2]));
Upvotes: 5