Jump to content

elegant way to preg_match more than one word?


Jalz

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.