2

I have a string like this:

$str = "it is a test";

I want to check it for these words: it, test. I want to it returns true if there is at least one of those words in the string.

Here is what I did: (though it does not work)

$keywords = array ('it', 'test');
if(strpos($str, $keywords) !== false){ echo 'true';}
else echo 'false';

How can I do that?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
  • 1
    Maybe this can help? http://stackoverflow.com/questions/19445798/php-check-if-string-contains-a-value-in-array – Stavros Nov 11 '15 at 15:38
  • I don't suppose you've bothered looking at the rather obscurely-named [str_word_count()](http://www.php.net/manual/en/function.str-word-count.php) function.... `$matches = array_intersect($keywords, str_word_count($str, 1));` – Mark Baker Nov 11 '15 at 15:41
  • Possible duplicate of [Check if a string contain multiple specific words](http://stackoverflow.com/questions/15862361/check-if-a-string-contain-multiple-specific-words) – swidmann Nov 11 '15 at 15:41

3 Answers3

8

Simply checking using preg_match(), you can add many different words in the pattern, just use a separator | in between words.

The following will match partial words, so larger words like pit, testify, itinerary will be matched. The pattern is also case-sensitive, so It and Test will not be matched.

$str = "it is a test";
if (preg_match("/it|test/", $str))
{
    echo "a word was matched";
}

Sorry, I didn't know that you were dealing with other languages, you can try this:

$str = "你好 abc efg";
if (preg_match("/\b(你好|test)\b/u", $str))
{
    echo "a word was matched";
}

I also need to mention that \b means word boundary, so it will only matches the exact words.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Andrew
  • 2,810
  • 4
  • 18
  • 32
  • It does not work for Persian language. Anyway thanks, +1 – Shafizadeh Nov 11 '15 at 15:44
  • @mickmackusa I was just a php hobbyist and honestly I haven't code for a very long time since then. Please feel free to edit my answer to give a better experience to any visitors :) – Andrew Nov 16 '20 at 10:33
1

The easiest way would be to user the explode function, this looks like:

$str = "it is a test"; // Remember your quotes!

$keywords = array ('it', 'test');

$str_array = explode(" ", $str);
$foundWords = [];
foreach ($keywords as $key)
{
    if (in_array($key, $str_array))
    {
        $foundWords[] = $key;
    }
}
foreach($foundWords as $word)
{
    print("Word '{$word}' was found in the string '{$str}'<br />");
}

This is a function with printing also

This gave me the result:

Word 'it' was found in the string 'it is a test'
Word 'test' was found in the string 'it is a test'

I think the issue with your code is that it is trying to match an array as a whole to the string, try doing it inside a foreach loop.

Another way would be something such as:

$keywords = array ('it', 'test');
echo (strpos($srt, $keywords[0]) ? "true" : "false");
echo (strpos($srt, $keywords[1]) ? "true" : "false");
Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • I feel there is a short solution. but ok thanks, +1. Actually I want to it checks first word in the string if it was exist, then break and return `true`. – Shafizadeh Nov 11 '15 at 15:39
  • There are many other solutions, but this is one that gives a good overview of what is required, in a clear and readable way, hope it helps :) – Can O' Spam Nov 11 '15 at 15:40
  • @Sajad, I have added another method that is both shorter and faster than my previous, maybe this is more what you are looking for, hope it helps :) – Can O' Spam Nov 11 '15 at 16:01
-1

I am not sure, so sorry of i am wrong. I think strpos doesnt work with arrays?

Try to do:

$array = ('it', 'test');
for($i=0;$i<$array.length;$i++){

//here the strpos Method but with $array[$i] }

Bernhard Miehl
  • 346
  • 1
  • 5
  • 15
  • Your array decloration is wrong I'm afraid, it should be: `$array = [ 'it', 'test' ]`, arrays use square crackets, not round :) – Can O' Spam Nov 11 '15 at 16:03
  • Y sorry i am writing from my mobilephone ^^ – Bernhard Miehl Nov 11 '15 at 16:04
  • round is fine, I use round one since bracket only available after php 5.4.0, http://php.net/manual/en/language.types.array.php#example-95, but you need the word `array` before the round bracket. – Andrew Nov 11 '15 at 16:13
  • 1
    @Andrew, thank you for that, something I had missed too :) – Can O' Spam Nov 11 '15 at 16:16
  • But in my opinion you still should use the square brackets, cause a lot of server are still running PHP below 5.4, i spent 4 hours of work to find out that my server is running below 5.4 :( – Bernhard Miehl Nov 11 '15 at 16:51