nomadsoul Posted November 11, 2009 Share Posted November 11, 2009 Hi, I tested this expression to see if it works before I use it for validating. I don't understand why it returns: "invalid email id" Can anyone tell me? I'm not very good with regular expressions. $email = "myemail@gmail.com"; if (ereg('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$','$email')) { echo 'Valid email id'; } else { echo 'Invalid email id'; } Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/ Share on other sites More sharing options...
cags Posted November 11, 2009 Share Posted November 11, 2009 ereg is a deprecated function, you should probably look at using preg_match instead. But I believe your problem is where you match the fullstop between the domain and the domain extension. Since the fullstop is not escaped it will match any character, which will mess up the pattern. but a backslash in before it should fix that. Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955549 Share on other sites More sharing options...
.josh Posted November 11, 2009 Share Posted November 11, 2009 ereg is a deprecated function, you should probably look at using preg_match instead. But I believe your problem is where you match the fullstop between the domain and the domain extension. Since the fullstop is not escaped it will match any character, which will mess up the pattern. but a backslash in before it should fix that. not escaping the dot would not break it per se (it wouldn't break it in this instance, anyways). It'll match (most) anything including a literal dot. Danger of not escaping it is that it can match more than a literal dot. anywhoo...your regex is failing because of this: '$email' You have $email wrapped in single quotes which causes ereg to interpret literally. Remove the quotes. Also as cags mentioned, move to preg_match instead of ereg. Only thing you really need to change for that is you need to add delimeters to your regex. Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955799 Share on other sites More sharing options...
cags Posted November 11, 2009 Share Posted November 11, 2009 You're right, I was thinking of matching an email within as string not matching an e-mail as a string (ie I was ignoring the ^ and $). So with everything being greedy it would match pretty much anything after the @ (except line breaks). Good catch on the single quotes, I didn't even notice that. Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955805 Share on other sites More sharing options...
.josh Posted November 11, 2009 Share Posted November 11, 2009 You're right, I was thinking of matching an email within as string not matching an e-mail as a string (ie I was ignoring the ^ and $). So with everything being greedy it would match pretty much anything after the @ (except line breaks). Good catch on the single quotes, I didn't even notice that. took me a min... I was about to vote for suggesting he trim $email then at last second noticed. Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955810 Share on other sites More sharing options...
nomadsoul Posted November 11, 2009 Author Share Posted November 11, 2009 Thanks, I didn't know it was deprecated. I will apply the advice. Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955833 Share on other sites More sharing options...
nomadsoul Posted November 11, 2009 Author Share Posted November 11, 2009 I tried: $email = "myemail@mydomain.com"; if (preg_match('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',"$email")) { echo 'Valid email id'; } else { echo 'Invalid email id'; } and now getting: Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in....myfile etc Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955845 Share on other sites More sharing options...
.josh Posted November 11, 2009 Share Posted November 11, 2009 Also as cags mentioned, move to preg_match instead of ereg. Only thing you really need to change for that is you need to add delimeters to your regex. Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955848 Share on other sites More sharing options...
cags Posted November 11, 2009 Share Posted November 11, 2009 One difference between the preg functions and the older functions is that patterns must have delimiters. It thinks you are using the ^ character as the delimiter since that is the first character. But you actually wish that to be the first character of your pattern. You simply need to add delimiters... if (preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$~', $email)) ... incidently I see you change the single quotes around $email to double quotes. You really might aswell just get rid of the quotes altogether (as I did in my example). Note: The delimiter does not have to be the ~ character, that is just the one I choose. Check out the manual for more details. Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955849 Share on other sites More sharing options...
nrg_alpha Posted November 12, 2009 Share Posted November 12, 2009 Note: The delimiter does not have to be the ~ character, that is just the one I choose. Check out the manual for more details. Studies have shown that real regex men use hash instead (and I'm not only refering to delimiters either). Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955906 Share on other sites More sharing options...
.josh Posted November 12, 2009 Share Posted November 12, 2009 Note: The delimiter does not have to be the ~ character, that is just the one I choose. Check out the manual for more details. Studies have shown that real regex men use hash instead (and I'm not only refering to delimiters either). ooh I think you finally won me over to using # instead of ~ I can say "I like hash" Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955914 Share on other sites More sharing options...
nomadsoul Posted November 12, 2009 Author Share Posted November 12, 2009 if (preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$~', $email)) Worked! Thanks. Will Zen(v) this for a while. Some good regexp info at PHPFKS. About the hash: "I tried smoking hash but the corned beef wouldn't stay lit" -forgot who said that originally Quote Link to comment https://forums.phpfreaks.com/topic/181104-solved-reg-expression-question/#findComment-955976 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.