Jump to content

Problem with date regex


lilnow

Recommended Posts

Hi

I am new to php and regex. I have a school exercise - I'm pretty sure the problem is in my $match variable, but lack the experience to see where I have gone wrong. I get a "Delimiter must not be alphanumeric or backslash". Any suggestions would be appreciated. Thanks.

 

// format for birthday of MMM-DD-YYYY. The year cannot be later than the current year.

$birthdate = $_GET["birth"];

$birth31days="/^(JAN|MAR|MAY|JUL|AUG|OCT|DEC)-([0-2][0-9] | [3][01])$/i";

$birth30days="/^(APR|JUN|SEP|NOV)-([0-2][0-9] | [3][0])$/i";

$birth29days="/^(FEB)-([0-2][0-9])$/i";

$years="/^19[0-9][0-9 ]$| ^20(0[0-9]| 1[01])$/";

$match = (($birth31days|$birth30days|$birth29days)-($years));

echo $match;

$birthRegex = $match;

if (preg_match($birthRegex, $birthdate)){              <-- error message implies it's here

echo "Birthday: $birthdate <br />";

}

else{

echo "Format incorrect. You entered $birthdate. Please go back and try again <br />";

}

Link to comment
Share on other sites

you have delimiters and modifiers in each of your $birth...(and $years) variables, treating them as individual patterns, but then put them all together in $match and treat it as a single pattern. You need to remove the delimiters and modifiers from those variables and only have the delimiter and modifier appear once, wrapped around the whole pattern.

 

Also, your $match variable...you need to wrap that entire thing in quotes...the way you have it now, php attempts to parse that as a mathematical expression. You should have seen from your own echo $match that something was funky...

Link to comment
Share on other sites

Thanks for the suggestion Crayon Violet. I merged the four variables and ended up with this - and it works!

'/^(((feb)\-((0[1-9])|([12][0-9])))|((apr|jun|sep|nov)\-((0[1-9])|([12][09])|30))|((jan|mar|may|jul|aug|oct|dec)\-((0[1-9])|([12][09])|(3[01]))))\-((19[0-9][0-9])|(20((0[0-9])|(1[01]))))$/i';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.