I have this piece of code in the middle of my web application.
if (preg_match ("/^[-0-9]/", $strDate) == TRUE)
{
$dateParts = explode("-", $strDate);
if (count($dateParts) === 3)
{
try
{
if (checkdate($dateParts[0], $dateParts[1], $dateParts[2]) === TRUE)
{
return TRUE;
}
}
catch (Exception $e)
{
//purposely blank for now.
}
}
}
I haven't included any surrounding code because it's a fairly lengthy app. To start, the $strDate
variable I know exactly what is held in there. I can echo it out right before the if
statement begins. If $strDate
is a valid date or any combination of 'TBD' the statement validates as expected. The problem comes when an invalid date enters the function.
For instance,
$strDate = '03-01-2017asdf';
With this input I would expect the preg_match
to catch it and kick it out of the if
statement. Instead it runs the try-except and php throws a non formed number error. Any ideas what I am doing wrong here? I'm sure it has to do with my preg_match
statement.