prezident Posted January 12, 2011 Share Posted January 12, 2011 I've tried alot of different ways, google, but just can't find the correct way to do this.. I'm trying to search for a word in an inputted string.. here's the code <html> <head><title>TESTING</title></head> <body> <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>"/> email: <input type=text name=email value="" /><br/> feedback: <input type=text name=ex value=""/><br/> <input type="submit" value="submit" /><br/> </body> </html> <?php $email = $_GET['email']; $ex = $_GET['ex']; $pattern = '/^[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z]+$/'; //$pattern1 = '/nprezident/prezident/'; if(!preg_match($pattern, $email)){ echo 'invalid email address<br/>'; } else { echo 'good to go<br/>'; } if(preg_match('/nprezident/,/prezident/', $ex)){ echo 'just something about that sentence is nice'; } else { echo 'no good'; } ?> Ok the one I'm having a problem with is the second preg_match when trying to search for two words i can get one word to print but when i use add a second word it gives an error. How do i add a second word? Quote Link to comment https://forums.phpfreaks.com/topic/224189-preg_match/ Share on other sites More sharing options...
cyberRobot Posted January 12, 2011 Share Posted January 12, 2011 Just to clarify, with the following code: if(preg_match('/nprezident/,/prezident/', $ex)){ Are you looking for "nprezident" or "prezident" in $ex? If so, you'll want to change the code to: if(preg_match('/(nprezident|prezident)/', $ex)) { Quote Link to comment https://forums.phpfreaks.com/topic/224189-preg_match/#findComment-1158382 Share on other sites More sharing options...
prezident Posted January 12, 2011 Author Share Posted January 12, 2011 thank you that works fine i was looking for either or ... Quote Link to comment https://forums.phpfreaks.com/topic/224189-preg_match/#findComment-1158387 Share on other sites More sharing options...
cyberRobot Posted January 12, 2011 Share Posted January 12, 2011 No problem. If you're looking for a quick reference for regular expressions, I find the following resource useful: http://weblogtoolscollection.com/regex/regex.php Quote Link to comment https://forums.phpfreaks.com/topic/224189-preg_match/#findComment-1158388 Share on other sites More sharing options...
Adam Posted January 12, 2011 Share Posted January 12, 2011 Regular expressions are meant for complex string matching, this is basic: if (strpos('nprezident', $ex) !== false || strpos('prezident', $ex) !== false) { It's arguably a little longer to write, but there's no need to use regular expressions for something so simple. Quote Link to comment https://forums.phpfreaks.com/topic/224189-preg_match/#findComment-1158391 Share on other sites More sharing options...
cyberRobot Posted January 12, 2011 Share Posted January 12, 2011 @MrAdam - good point Quote Link to comment https://forums.phpfreaks.com/topic/224189-preg_match/#findComment-1158394 Share on other sites More sharing options...
Maq Posted January 12, 2011 Share Posted January 12, 2011 It's arguably a little longer to write, but there's no need to use regular expressions for something so simple. I agree. And rule of thumb is if you don't need regex you should try to avoid it. Quote Link to comment https://forums.phpfreaks.com/topic/224189-preg_match/#findComment-1158403 Share on other sites More sharing options...
prezident Posted January 14, 2011 Author Share Posted January 14, 2011 thank you all Quote Link to comment https://forums.phpfreaks.com/topic/224189-preg_match/#findComment-1159345 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.