知道Array中是否存在特定的单词?

时间:2012-07-20 17:39:37

标签: php

我有一个数组,我想知道它是否包含一个特定的单词或者不是例如microsoft-iis 7.5?

Array
(
[0] => HTTP/1.1 302 Found
[1] => Cache-Control: no-cache, no-store, must-revalidate, no-transform
[2] => Pragma: no-cache
[3] => Content-Length: 340
[4] => Content-Type: text/html; charset=utf-8
[5] => Expires: -1
[6] => Server:microsoft-iis 7.5
)

谢谢

4 个答案:

答案 0 :(得分:1)

if(array_walk($array, function($str){ return strstr($str, 'microsoft-iss 7.5'); })){
    echo "array_matches";
}

答案 1 :(得分:1)

如果你只想知道字符串是否存在,你可以在内爆字符串上运行stripos:

if (false !== stripos(implode("\n", $headers), "microsoft-iis"))
{
   // ...
}

(如果你想要区分大小写,则为strpos。)

答案 2 :(得分:1)

$stack = Array(
          '0' => 'HTTP/1.1 302 Found',
          '1' => 'Cache-Control: no-cache, no-store, must-revalidate, no-transform',
          '2' => 'Pragma: no-cache',
          '3' => 'Content-Length: 340',
          '4' => 'Content-Type: text/html; charset=utf-8',
          '5' => 'Expires: -1',
          '6' => 'Server:microsoft-iis 7.5'
);

$contain = substr_count ( implode( $stack ), 'microsoft-iis 7.5' )?'string found':'string not found';

echo $contain;

答案 3 :(得分:0)

你可以迭代数组并使用substr_count来检查。它将返回搜索字符串出现的次数

foreach ($your_array as $values){
$occurrence = substr_count($values,'microsoft-iis 7.5') ? 'exists' : 'does not exist';
    echo $occurrence;
}