0

Hi I'm trying to check if a list of words (or any one of them) exists in a string. I tried some of the examples i found here, but i still can't get it to work correctly.

Any ideas what I'm doing wrong?

$ss3="How to make a book";
    $words = array ("book","paper","page","sheet");
    if (in_array($ss3, $words) )
{
    echo "found it"; 
}
1
  • in_array will only checks the complete string value in an array.
    – devpro
    Commented Dec 15, 2015 at 10:02

7 Answers 7

2

Loop over your array, check for each element if it exists in the string

$ss3="How to make a book";
$words = array ("book","paper","page","sheet");
foreach($words as $w){
  if (stristr($ss3,$w)!==false)
   echo "found $w \n"; 
}

Fiddle

0

You need to explode() your $ss3 string and then compare each item with your $words with loop

manual for in_array - http://php.net/manual/en/function.in-array.php

manual for explode() - http://php.net/manual/ru/function.explode.php

$matches = array();
$items = explode(" ",$ss3);
foreach($items as $item){
  if(in_array($item, $words)){         
     $matches[] = $item; // Match found, storing in array
  }
} 

var_dump($matches); // To see all matches
1
  • Note: This answer is invalid in terms of output. We have to check whether any or all of the elements of $words are in the string, and not the other way around. For example this answer will say to is not in words but the OP doesn't want to check for that. They need to check only for the words contained in $words, please consider updating accordingly Commented Dec 15, 2015 at 10:36
0

This is how you will check for existence of a word from a string. Also remember that you must first convert the string to lowercase and then explode it.

$ss3="How to make a book";
$ss3 = strtolower($ss3);
$ss3 = explode(" ", $ss3);
    $words = array ("book","paper","page","sheet");
    if (in_array($ss3, $words) )
{
    echo "found it"; 
}

Cheers!

0

You could use regular expressions. It would look something like this:

$ss3 = "How to make a book";

 if (preg_match('/book/',$ss3))
     echo 'found!!';
0

in_array will only check the complete string value in an array. Now you can try this:

$string = 'How to make a book';
$words = array("book","paper","page","sheet");
foreach ($words as $val) {
    if (strpos($string, $val) !== FALSE) {
        echo "Match found"; 
        return true;
    }
}
echo "Not found!";
0

You can use str_word_count along with array_intersect like as

$ss3="How to make a book";
$words = array ("book","paper","page","sheet");
$new_str_array = str_word_count($ss3,1);
$founded_words = array_intersect($words,$new_str_array);
if(count($founded_words) > 0){
    echo "Founded : ". implode(',',$founded_words);
}else{
    echo "Founded Nothing";
}

Demo

0

This code will help you for better answer

    <?php
    $str="Hello World Good";
    $word=array("Hello","Good");
    $strArray=explode(" ",$str);
   foreach($strArray as $val){
   if(in_array($val,$word)){
   echo $val."<br>";
   }
   }

 ?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.