4

Must be simple but cant find my answer.
How to test whether one of the values in the array is contained in the string?
Output should 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 array[3] should not be a match. Only array[2] should be.

5
  • Do you want to match whole words or just check whether the substrings in the are are anywhere in the string? Commented May 31, 2011 at 14:47
  • check if substrings are anywhere in the string Commented May 31, 2011 at 14:52
  • possible duplicate of php test if string contains one of three strings? Commented May 31, 2011 at 14:54
  • Regarding your edit: Then it is not anywhere in the string, it has to match word boundaries. Commented May 31, 2011 at 14:57
  • @Felix Kling, didn't thought of that, edited again the right way Commented May 31, 2011 at 15:15

7 Answers 7

2

The functional replacement for your in_array would be:

array_filter(
    array_map("strpos",  array_fill(0, count($words), $string), $words),
"is_int")
Sign up to request clarification or add additional context in comments.

Comments

1

Update:

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

$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 looping over the terms:

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

Both can be put in a function to simplify the use, e.g.

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

Have a look at a DEMO


Old answer:

You could 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 are the boundary characters \b here. You will only get a match if the value you search for is a (sequence of) word(s) in the string.

2 Comments

ty, but it could also be for more then one word edited example in op.
@Rob: Please have a look at my update. I think this will help you.
1

A brute force method would be:

$words = implode('|', $array);

if (preg_match("/($words)/", $string, $matches)) {
    echo "found $matches[1]";
}

1 Comment

Don't forget to use preg_quote.
0
$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>";
}

Comments

0

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

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

1 Comment

Uh, whoops? Must have read over this part: The code I've got work only works for an exact match (and is the wrong way) so output here would be false but I need a true... Sorry
0
$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/>';
    }
}

Comments

0

Something like this?

$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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.