ZachMEdwards Posted June 20, 2010 Share Posted June 20, 2010 I'm looking for a pattern that will match everything but the words "Peanut Butter". Thanks! Quote Link to comment Share on other sites More sharing options...
Alex Posted June 20, 2010 Share Posted June 20, 2010 You mean take out everything out of a string that is not "Peanut Butter" or match a string if it does not contain "Peanut Butter"? Either way you don't need regex for this. If the former just to str_replace to remove "Peanut Butter" from the string and you have everything that's left. If the latter and you just want to see if a string does or does not contain "Peanut Butter" use strpos. Quote Link to comment Share on other sites More sharing options...
ZachMEdwards Posted June 20, 2010 Author Share Posted June 20, 2010 I got it: $pattern = '/I like ((?!Peanut Butter).)+/'; That matches: I like cream cheese I like toy cars BUT IT DOESN'T MATCH: I like Peanut Butter Thanks Quote Link to comment Share on other sites More sharing options...
Alex Posted June 20, 2010 Share Posted June 20, 2010 There's no need to load the memory expensive regular expression engine for something like this. if(strpos($string, 'Peanut Butter') !== false) { // contains Peanut Butter } else { // doesn't contain Peanut Butter } Quote Link to comment 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.