开发者

PHP How to test whether one of the values of an array exists (partly) in a string?

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.


The functional replacement for your in_array would be:

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


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.


A brute force method would be:

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

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


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


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!';
}


$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/>';
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜