1

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!

2
  • Can you please show your full code; not just the pattern you're working with. Commented Dec 15, 2012 at 1:41
  • 1
    stackoverflow.com/questions/141315/… Commented Dec 15, 2012 at 1:43

1 Answer 1

5
$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]));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.