lilnow Posted February 22, 2011 Share Posted February 22, 2011 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 />"; } Quote Link to comment https://forums.phpfreaks.com/topic/228462-problem-with-date-regex/ Share on other sites More sharing options...
.josh Posted February 22, 2011 Share Posted February 22, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/228462-problem-with-date-regex/#findComment-1178174 Share on other sites More sharing options...
lilnow Posted February 27, 2011 Author Share Posted February 27, 2011 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'; Quote Link to comment https://forums.phpfreaks.com/topic/228462-problem-with-date-regex/#findComment-1180432 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.