PHP How to check if one of the array values ​​exists (partially) in a string?

It should be simple, but I can not find the answer.
How to check if one of the values ​​is contained in an array in a string?
The output must be true or false.

$array = Array( 0 => 'word1', 1 => 'word2', 2 => 'New York' 3 => 'New car' ); $string = "Its a sunny day in New York"; 

Trying to clarify. In this case, the array [3] should not match. Only array [2] should be.

+4
source share
7 answers

Update:

A solution independent of the word boundary would be to add spaces around the input line and the search word:

 $str = ' ' . $str . ' '; function quote($a) { return ' ' . preg_quote($a, '/') . ' '; } $word_pattern = '/' . implode('|', array_map('quote', $array)) . '/'; if(preg_match($word_pattern, $str) > 0) { } 

or by cyclizing the terms:

 foreach($array as $term) { if (strpos($str, ' '. $term . ' ') !== false) { // word contained } } 

Both can be placed in a function for ease of use, for example.

 function contains($needle, $haystack) { $haystack = ' ' . $haystack . ' '; foreach($needle as $term) { if(strpos($haystack, ' ' . $term . ' ') !== false) { return true; } } return false; } 

Look demo


Old answer:

You can use regular expressions:

 function quote($a) { return preg_quote($a, '/'); } $word_pattern = implode('|', array_map('quote', $array)); if(preg_match('/\b' . $word_pattern . '\b/', $str) > 0) { } 

The important part here is the \b boundary characters. You will only get a match if the value you are looking for is a (sequence) of words (words) per line.

+1
source

The functional replacement for in_array will be as follows:

 array_filter( array_map("strpos", array_fill(0, count($words), $string), $words), "is_int") 
+2
source

Brute Force Method:

 $words = implode('|', $array); if (preg_match("/($words)/", $string, $matches)) { echo "found $matches[1]"; } 
+1
source
 $array = Array( 0 => 'word1', 1 => 'word3', 2 => 'word3 basic', 3 => 'good' ); $string = "there a good word3 basic here"; //Convert the String to an array $stringArray = explode(' ',$string); //Loop the string foreach($stringArray as $matchedWords) { if(in_array($matchedWords, $array )) { echo $matchedWords.'<br/>'; } } 
0
source
 $array = Array( 0 => 'word1', 1 => 'word2', 2 => 'word3' ); $string = "there a good word3 here"; foreach($array as $word) { if(strstr($string, $word)) echo "<b>$word</b> has been detected in <b>$string</b>"; } 
0
source

You can use the in_array function for this: http://php.net/manual/en/function.in-array.php

 if (in_array($value, $array)) { echo $value . ' is in the array!'; } 
0
source

Something like that?

 $array = Array( 0 => 'word1', 1 => 'word2', 2 => 'word3' ); $string = "there a good word3 here"; function findInArray($string, $array) { for ($x=0; $x < count($array); $x++) { if (preg_match('/\b' . $array[$x] . '\b/', $string)) { // The \b in the pattern indicates a word boundary, so only the distinct return true; } } return false; } if (findInArray($string, $array)) { // do stuff } 
0
source

All Articles