php 字符串包含数组中的任何项目(不区分大小写)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2124527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:09:04  来源:igfitidea点击:

String contains any items in an array (case insensitive)

phpstringarrayscontains

提问by tarnfeld

How can i check if a $stringcontains any of the items expressed in an array?

如何检查 a 是否$string包含数组中表示的任何项目?

$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(contains($string,$array))
{
// do something to say it contains
}

Any ideas?

有任何想法吗?

采纳答案by sbczk

is that what you wanted? i hope that code is compiling :)

那是你想要的吗?我希望代码正在编译:)

$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(0 < count(array_intersect(array_map('strtolower', explode(' ', $string)), $array)))
{
  //do sth
}

回答by zombat

I don't think there is a built-in function that will handle what you want. You could easily write a contains()function however:

我不认为有一个内置函数可以处理你想要的。但是,您可以轻松编写一个contains()函数:

function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (stripos($str,$a) !== false) return true;
    }
    return false;
}

回答by kkonstantinov

Using the accepted answer:

使用接受的答案:

$string = 'My nAmE is Tom.';
$array = array("name","tom");
if(0 < count(array_intersect(array_map('strtolower', explode(' ', $string)), $array)))
{
  //do sth
}

Just a side note that the if statement could be changed to:

只是附带说明一下,if 语句可以更改为:

if(0 < count(array_intersect(explode(' ', strtolower($string)), $array)))

since it's not really necessary to use array_map to apply strtolowerto each element. instead apply it to the initial string.

因为实际上没有必要使用 array_map 来应用于strtolower每个元素。而是将其应用于初始字符串。

回答by Ivan Ternovtsiy

One more workaround for contains function

contains 函数的另一种解决方法

function contains($string, $array, $caseSensitive = true)
{
    $stripedString = $caseSensitive ? str_replace($array, '', $string) : str_ireplace($array, '', $string);
    return strlen($stripedString) !== strlen($string);
}

PS. as for me, I'm just use it without function..

附注。至于我,我只是在没有功能的情况下使用它..

if (strlen(str_replace($array, '', $string)) !== strlen($string)) {
    // do it
}

回答by Paul Osman

Something like this would work:

像这样的事情会起作用:

$string = 'My nAmE is Tom.';
$array = array("name", "tom");
foreach ($array as $token) {
    if (stristr($string, $token) !== FALSE) {
        print "String contains: $token\n";
    }
}

回答by Jaydeep Gondaliya

<?php

$input = preg_quote('blu', '~'); // don't forget to quote input string!
$data = array('orange', 'blue', 'green', 'red', 'pink', 'brown', 'black');

$result = preg_grep('~' . $input . '~', $data);
print_r($result);

?>

回答by Irshad Khan

Another way to do with array_intersect() function, Try below code :

使用 array_intersect() 函数的另一种方法,试试下面的代码:

function checkString(array $arr, $str) {

  $str = preg_replace( array('/[^ \w]+/', '/\s+/'), ' ', strtolower($str) ); // Remove Special Characters and extra spaces -or- convert to LowerCase

  $matchedString = array_intersect( explode(' ', $str), $arr);

  if ( count($matchedString) > 0 ) {
    return true;
  }
  return false;
}

回答by jspcal

function contains($str, $arr)
{
  $ptn = '';
  foreach ($arr as $s) {
    if ($ptn != '') $ptn .= '|';
    $ptn .= preg_quote($s, '/');
  }
  return preg_match("/$ptn/i", $str);
}

echo contains('My nAmE is Tom', array('name', 'tom'));

回答by robertbasic

Will this do the job?

这会完成这项工作吗?

$words = explode(" ", $string);
$wordsInArray = array();
foreach($words as $word) {
    if(in_array($word, $array)) {
        $wordsInArray[] = $word;
    }
}

回答by Alejandro Moreno

Much simpler, please refer the link in_array

更简单,请参考in_array链接

$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Es Irix";
}