检查字符串是否包含数组的任何值?

Check if string contains any value of an array?

$search=array("<",">","!=","<=",">=")
$value="name >= vivek ";

我想检查 $value 是否包含 $search 数组的任何值。我可以找到使用 foreach 和 strpos 函数。不求助于foreach,我还能找到答案吗?如果是这样,请帮助我解决这个问题。

是的,但这需要您重新构建代码。

$search = array("<" => 0, ">" => 1,"!=" => 2,"<=" => 3,">=" => 4);

$value = "name => vivek ";

$value = explode(" ", $value);

foreach($value as $val) {
    // search the array in O(1) time
    if(isset($search[$val])) {
       // found a match
    }
}

使用array_map() 和array_filter()

function cube($n)
{
    $value="name => vivek ";
    return strpos($value, $n);
//return($n * $n * $n);
}

$a = array("<",">","!=","<=",">=");
$value="name => vivek ";

$b = array_map("cube", $a);
print_r($b);

$b = array_filter($b);

print_r($b);
$search  = array("<",">","!=","<=",">=");
$value="name => vivek ";
foreach($value as $searchval) {
  if(strpos($value,  $searchval) == false)
  {
  echo "match not found";
  }
  else
  {
  echo "match found";
  }
}

这是一个使用 array_reduce 的解决方案:

<?PHP
function array_in_string_callback($carry, $item)
{
    list($str, $found) = $carry;
    echo $str . " - " . $found . " - " . $str . " - " . $item . "<br/>";
    $found |= strpos($str, $item);
    return array($str, (boolean) $found);
}

function array_in_string($haystack, $needle)
{
    $retVal = array_reduce($needle, "array_in_string_callback", array($haystack, false));
    return $retVal[1];
}

$search=array("<",">","!=","<=",">=");
$value="name >= vivek ";

var_dump(array_in_string($value, $search));
?>

分解$value并将其转换为数组,然后使用php中的array_intersect()函数检查字符串是否不包含array.Use代码的值以下

    <?php
    $search=array("<",">","!=","<=",">=");
    $value='name >= vivek ';
    $array = explode(" ",$value);

    $p = array_intersect($search,$array);
    $errors = array_filter($p);
//Check if the string is not empty
    if(!empty($errors)){
    echo "The string contains an value of array";
    }
    else
    {
    echo "The string does not containe the value of an array";
    }

    ?>

在此处测试代码http://sandbox.onlinephpfunctions.com/code/7e65faf808de77036a83e185050d0895553d8211

希望对您有所帮助

我的第一个想法是使用 array_walk() 和回调来解决问题,如下所示:

<?php

$search=array("<",">","!=","<=",">=");
$value = "name >= vivek ";

function test($item, $key, $str)
{
    if( strpos($str, $item) !== FALSE ) {
        echo "$item found in \"$str\"\n";        
    }
}

array_walk($search, 'test', $value);
// output:
> found in "name >= vivek "
>= found in "name >= vivek "

现场演示:http://3v4l.org/6B0WX

虽然这在没有 foreach 循环的情况下解决了问题,但它用 "what" 而不是 "yes/no" 响应来回答问题。以下代码直接回答问题并允许回答 "what",如下所示:

<?php

function test($x)
{
    $value="name >= vivek ";
    return strpos($value, $x);
}
$search = array("<",">","!=","<=",">=");
$chars = array_filter( $search, "test" );
$count = count($chars);
echo "Are there any search chars? ", $answer = ($count > 0)? 'Yes, as follows: ' : 'No.';
echo join(" ",$chars);

// output:
Are there any search chars? Yes, as follows: > >=

现场演示:http://3v4l.org/WJQ5c

如果响应是否定的,则输出为 'No.',后跟一个空白 space。

与第一个解决方案相比,第二个解决方案的一个关键区别在于,在这种情况下,有一个 return 可以操纵的结果。如果数组的一个元素与字符串中的一个字符匹配,则 array_filter 将该元素添加到 $chars。新数组的元素计数回答了这个问题,数组本身包含任何匹配项,如果有人希望显示它们的话。