PHP 如何测试数组的值之一是否(部分)存在于字符串中?

发布于 2024-11-12 23:24:23 字数 296 浏览 1 评论 0原文

一定很简单,但找不到我的答案。
如何测试数组中的某个值是否包含在字符串中?
输出应该是 true 或 false。

$array = Array( 
   0 => 'word1',
   1 => 'word2',
   2 => 'New York'
   3 => 'New car' 
);

$string = "Its a sunny day in New York";

试图澄清。在这种情况下 array[3] 不应该匹配。只有 array[2] 应该是。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

天暗了我发光 2024-11-19 23:24:24

in_array 的功能替换是:

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

The functional replacement for your in_array would be:

array_filter(
    array_map("strpos",  array_fill(0, count($words), $string), $words),
"is_int")
想挽留 2024-11-19 23:24:24

更新:

独立于单词边界的解决方案是在输入字符串和搜索词周围添加空格:

$str = ' ' . $str . ' ';


function quote($a) {
    return ' ' . preg_quote($a, '/') . ' ';
}

$word_pattern = '/' . implode('|', array_map('quote', $array)) . '/';

if(preg_match($word_pattern, $str) > 0) {

}

或者通过循环术语:

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

两者都可以放入函数中以简化使用,例如

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

有一个看看 DEMO


旧答案:

您可以使用常规表达式:

function quote($a) {
    return preg_quote($a, '/');
}

$word_pattern = implode('|', array_map('quote', $array));

if(preg_match('/\b' . $word_pattern  . '\b/', $str) > 0) {

}

重要的部分是边界字符\b 在这里。仅当您搜索的值是字符串中的单词(序列)时,您才会获得匹配项。

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.

半衾梦 2024-11-19 23:24:24

暴力方法是:

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

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

A brute force method would be:

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

if (preg_match("/($words)/", $string, $matches)) {
    echo "found $matches[1]";
}
梦情居士 2024-11-19 23:24:24
$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>";
}
$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>";
}
念三年u 2024-11-19 23:24:24

您可以使用 in_array 函数来实现:
http://php.net/manual/en/function.in-array.php

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

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!';
}
明明#如月 2024-11-19 23:24:24
$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/>';
    }
}
$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/>';
    }
}
黒涩兲箜 2024-11-19 23:24:24

像这样的东西吗?

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文