Link to home
Start Free TrialLog in
Avatar of NerdsOfTech
NerdsOfTechFlag for United States of America

asked on

PHP Sanitizing Date Inputs via REGEX for first instance of d/m/Y

Basically, I want to return the matching FIRST pattern of 1 or 2 digits (as the day), 1 or 2 (digits as the month), 4 digits as the year separated by forwarding slash characters '/' from a valid or malformed input.

given these hypothetical inputs:

01/01/2017
1/1/2017/01/03/2017
1/1/2017/01/03/2017/a/b/z/0000
a/1/2/b/c/9/1/g/8/99/1/34/9/99/2017/z
ab/cd/efgh

I would like the output to be:
01/01/2017
1/1/2017
1/1/2017
9/99/2017
FALSE
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NerdsOfTech

ASKER

[code]([0-2]?[0-9]|3[0-1])/([0]?[0-9]|1[0-2])/[1-2][0-9]{3}[/code] would be a step closer to valid dates but 99/99/9999 is ok as an input for this question. The extraction is ran through another function checkdate() to process the final validation. Thanks Dan for the extra $result = $regs[0]; line as this gives FIRST occurance. Thanks Bill for the first valid REGEX to match. I'll split points to both.