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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.