Jalz Posted April 8, 2010 Share Posted April 8, 2010 Hi hopefully a simple one here. I have the following line which searches for the word Cat in the animal session I have created. if (preg_match ("/\bCat\b/i", $_SESSION['animal'])) { do some commands. However I want to add alot more possible matches, i.e. Dog, Hamster, Cow, Horse etc etc. I thought I could use the following (which I haven't tried yet) if (preg_match ("/\bCat\b/i", $_SESSION['animal']) || preg_match ("/\bDog\b/i", $_SESSION['animal'] || preg_match ("/\bHamster\b/i", $_SESSION['animal']) { do some commands. Is this the way to filter for these words it or is there a shorthand I can use to filter out the 10 odd words I have. Much appreciate your help. Thanks Link to comment https://forums.phpfreaks.com/topic/197982-elegant-way-to-preg_match-more-than-one-word/ Share on other sites More sharing options...
salathe Posted April 8, 2010 Share Posted April 8, 2010 You can use alternation to look through a whole bunch of values. For example: /\b(?:cat|dog|hamster|horse|badger|mushroom|snake)\b/i Link to comment https://forums.phpfreaks.com/topic/197982-elegant-way-to-preg_match-more-than-one-word/#findComment-1038921 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.