-3

I am trying to search for certain word in a sentence. To that effect, I leverage solutions found (source), which works fine for searching just a word.

My issue is that I need to search the words in the array in a sentence something like

$findme = array("food", "water", "salt");

How can I do that?

Here is the code:

$mystring = 'my best food is Jellof Rice';


$findme   = 'food';
$pos = strpos($mystring, $findme);

if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'.";
}
else {
    echo "The string '$findme' was found in the string '$mystring',";
    echo " and exists at position $pos.";
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38

4 Answers4

-1

try something like this one

$mystring = 'text goes here';
$Found = false;
$findme = array("food", "water", "salt");
foreach($findme  as $singleValue ){
 if ($strpos($mystring, $singleValue ) != false) {
   $Found = true;
 }
}
Onkar
  • 2,409
  • 2
  • 11
  • 14
  • 2
    "Try this" does not make for a good answer. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](//stackoverflow.com/help/how-to-answer) – John Conde Jan 03 '21 at 16:16
-1

At the risk of also incurring hefty downvotes one possible method, if I understood correctly, would be to use a regExp and create a pattern using all the possible words from the $findme array.

$str='my fictional favourite food is Jellof Rice with salted water but not too much sugar and some rice';
#$str='banana apple mango';

$words=array( 'food', 'water', 'salt' );


function findmatches( $str=false, $search=array()){
    if( !empty( $str ) && !empty( $search ) ){
        # modify source array by quoting words if they require
        array_walk( $search, function( &$item ){ 
            $item=preg_quote( $item, '@' );
        });
        
        # generate the simple pattern by imploding all the words
        $pttn=sprintf('@(\w%s)@',implode( '|', $search ) );
        
        # try to capture all matches
        preg_match_all( $pttn, $str, $matches, PREG_OFFSET_CAPTURE );
        
        # return a suitable result - the frst set of matches or false if not found
        return !empty( array_filter( $matches ) ) ? $matches[0] : false;
    }
    return false;
}

function showresults( $arr ){
    # utility to display the results
    if( !empty( $arr ) ){
        foreach( $arr as $i => $v ){
            printf('The word "%s" was found at position %s',$v[0],$v[1]);
        }
    }
}


$res=findmatches( $str, $words );
echo $res ? showresults($res) : 'Not found';

Which will output

The word "salt" was found at position 48
Array
(
    [0] => salt
    [1] => 48
)
The word "water" was found at position 55
Array
(
    [0] => water
    [1] => 55
)

Live example

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
-2

just use a loop, you can do that in a function

function findIn($string, $array) {
    foreach($array as $item) {
        if(strpos($string, $item) === false) return false;
    }
    return true;
}
Nathanael
  • 870
  • 5
  • 11
-2

I would recommend to use a regex (PREG) expression.

  1. If you have a single string, test for matches in that string.
  2. If you have an array of strings with multiple occurrences, loop through each string in the array and apply the regex-expression.
  3. If you need each word-occurrence just once, store results as keys in an associtaive array.

Same problem is discussed here: Php find word in text using regular expression