tbare Posted December 6, 2007 Share Posted December 6, 2007 looking for the best way to compare a string, and if a sting != equal something, do something, else, go back and fix it. basically, i'm setting up an e-mail script, and i want to make sure the e-mail address entered is a valid, and not the default "[email protected]" any ideas as to what the best way to do this would be? Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/ Share on other sites More sharing options...
Orio Posted December 6, 2007 Share Posted December 6, 2007 ...? <?php if($str != "123") echo "str is not 123!"; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/#findComment-408002 Share on other sites More sharing options...
tbare Posted December 6, 2007 Author Share Posted December 6, 2007 that worked... now, what would (in your opinion) be the best way to check that the e-mail address is a valid one (containing a [blah]@[blah].[blah])? sorry for the noobish questions here, but i'm trying to learn as i go here... (as we all are, i know.) Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/#findComment-408014 Share on other sites More sharing options...
Orio Posted December 6, 2007 Share Posted December 6, 2007 Regex.. <?php if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) echo "Invalid email"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/#findComment-408019 Share on other sites More sharing options...
tbare Posted December 6, 2007 Author Share Posted December 6, 2007 <?php if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) echo "Invalid email"; ?> what exactly is that checking? Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/#findComment-408045 Share on other sites More sharing options...
Orio Posted December 6, 2007 Share Posted December 6, 2007 That will match an email pattern. This is a regular expression- basically you can say a regular defines a "dictionary" containing all of the "words" that follow the given pattern, and then checks if your "word" (string) is in that dictionary. The syntax of regular expressions isn't so difficult, and it's a very powerful tool, you should try and learn regular expressions (or in short- regex). Google will provide you alot of information. After you learn about regular expressions, the info in the manual will be a very good reference: http://www.php.net/manual/en/reference.pcre.pattern.syntax.php Orio. Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/#findComment-408088 Share on other sites More sharing options...
tbare Posted December 6, 2007 Author Share Posted December 6, 2007 thanks i'll check that out... other than not understanding exactly WHY it works (which i will), everything works great now! Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/#findComment-408114 Share on other sites More sharing options...
PHP_PhREEEk Posted December 6, 2007 Share Posted December 6, 2007 A regex cannot 'validate' an email address. It is a glorified spell checker, and that's all. It can only see if all the right parts exist for it to be an email address. Using the REGEX provided, if I entered [email protected] into your webform, it would be accepted, because it is in the right format for a email address. As long as you are aware of that... So, a REGEX can only catch a horrendous typo, or a submission that doesn't even try to look like an email. The only true validation is to send a 'challenge' email to the address they entered, and wait for them to respond to it. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/#findComment-408122 Share on other sites More sharing options...
tbare Posted December 6, 2007 Author Share Posted December 6, 2007 yeah... understood... i have it right now that if they left either of the default values in for either their e-mail or the recipient e-mail, it won't go through, then used the REGEX after that to make sure that it's in the right format... Thanks for the heads up, though... Quote Link to comment https://forums.phpfreaks.com/topic/80478-solved-sting-comparison-help/#findComment-408129 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.