jebediah Posted November 30, 2013 Share Posted November 30, 2013 Well, i hope this is ending up in the right place, if not i'm sorry. First of, i'm still not that familiar with php as i hope to be. I have a a slight problem with a registration code that i need help with. First. if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Fel: Ogiltig E-mail adress"; } if( empty($errors)) This code works just fine. But i i want to remove the if( empty($errors)) line. How would the code look like then? I've tried several solution but non of them works. Second. I have to add a new code similar to the $email_address code named something like $med_nr, but the entering option would look something like this S18757/2002 There's always a S in the beginning, 5 numbers then a / and then 4 numbers again. Any help with this is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/284403-if-preg_match/ Share on other sites More sharing options...
Irate Posted November 30, 2013 Share Posted November 30, 2013 (edited) RegExp for the 2nd question would look like this... #S[\d]{5}/[\d]{4}# As for the RegExp you posted, you can use commonly used shortcuts, like [\w] instead of [a-zA-Z_-], I think that'd help you somewhat. Edit: Forgot a backslash. Edited November 30, 2013 by Irate Quote Link to comment https://forums.phpfreaks.com/topic/284403-if-preg_match/#findComment-1460762 Share on other sites More sharing options...
jebediah Posted February 21, 2014 Author Share Posted February 21, 2014 RegExp for the 2nd question would look like this... #S[\d]{5}/[\d]{4}# Thanks for that and sorry for the old bump. If i would like to add a E to that, so both S18757/2002 and SE18757/2002 works, how would that look like? Quote Link to comment https://forums.phpfreaks.com/topic/284403-if-preg_match/#findComment-1469825 Share on other sites More sharing options...
Augury Posted February 21, 2014 Share Posted February 21, 2014 (edited) /^([E])?[\d]{5}/[\d]{4}$/ I'm guessing. Edited February 21, 2014 by Augury Quote Link to comment https://forums.phpfreaks.com/topic/284403-if-preg_match/#findComment-1469892 Share on other sites More sharing options...
Irate Posted February 24, 2014 Share Posted February 24, 2014 Thanks for that and sorry for the old bump. If i would like to add a E to that, so both S18757/2002 and SE18757/2002 works, how would that look like? Just add E? after the S. Quote Link to comment https://forums.phpfreaks.com/topic/284403-if-preg_match/#findComment-1470392 Share on other sites More sharing options...
.josh Posted February 24, 2014 Share Posted February 24, 2014 [] is a character class and will match any one thing in it. So doing something like this or [\d] is superfluous; you'd just put S or \d (or whatever) without the [] surrounding it. In fact, \d itself is a character class. It's shorthand for [0-9]. So to match for S or an E, you would have [sE]. But since you want to match an S optionally followed by an E, then you'd do SE?. Also, you're matching for a forward-slash but you're also using that as the pattern delimiter. So you have to escape the forward-slash in your pattern or else use a different pattern delimiter. I personally like using ~ since it rarely ever comes up, and / is pretty common because php is used with web stuff so matching paths and (x)html is common and / is a common symbol in them. # is also a popular alternative. So overall, you should just have ~^SE?\d{5}/\d{4}$~ As to your first question: First. if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Fel: Ogiltig E-mail adress"; } if( empty($errors)) This code works just fine. But i i want to remove the if( empty($errors)) line. How would the code look like then?I've tried several solution but non of them works. I'm going to assume that you tried just removing if( empty($errors)) and that "didn't work". Well that line isn't directly part of your email regex condition, so I can't tell you why it "didn't work" or how to remove it in a way that "works". You're going to have to explain what you mean by "didn't work". What is (not) happening that you (don't) want to happen? Quote Link to comment https://forums.phpfreaks.com/topic/284403-if-preg_match/#findComment-1470416 Share on other sites More sharing options...
jebediah Posted March 2, 2014 Author Share Posted March 2, 2014 Thanks for the help again. Everything works great Quote Link to comment https://forums.phpfreaks.com/topic/284403-if-preg_match/#findComment-1471255 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.