Jump to content

How to check a string for words in an array


etrader

Recommended Posts

I want to check if a string has any of words listed in array;

$array=array('word1', 'word2', 'word3');
$string="This is a sentence containing word2";

Now I want to make an 'if else' to check if the string contains any of the words listed in the array. Thanks  :shy:

Link to comment
Share on other sites

There's a few ways to check for items, it's all in how you set it up..what you are looking for..and what kind or results you expect. These are things you have to do unique to your project with making some checks and balances somehow.

 

Sometimes you need to use a mixture to get the exact desired results.

 

You may even have to explode the array and a foreach just to clean it up a bit before checking it.

 

some useful stuff

I usually stick to preg_match,preg_match_all,preg_quote,in_array for the most part.

http://php.net/manual/en/function.preg-match.php

http://www.php.net/manual/en/function.preg-match-all.php

http://www.php.net/manual/en/function.preg-quote.php

http://php.net/manual/en/function.array-search.php

 

The code that sasa gave is most likely what you need, you need to adjust the pattern for how you exactly want to look for items.

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.