","!=","<=",">=") $value="name >= vivek "; I want to check if the $ valu...">

Check if string contains any array value?

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

      

I want to check if the $ value contains the value of any of the values ​​in the $ search array. I can find out using foreach and strpos function. Without resorting to using foreach, can I find an answer? If yes then kindly help me to solve this problem.

+3


source to share


6 answers


Expand $value

and convert it to an array and then use a function array_intersect()

in php to check if a string does not contain an array value. Use below code

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

    ?>

      



Check the code here http://sandbox.onlinephpfunctions.com/code/7e65faf808de77036a83e185050d0895553d8211

Hope this helps you

+2


source


Yes, but it will require you to restructure your code.



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

      

0


source


Use array_map () and 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);

      

0


source


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

      

0


source


Here's a solution using 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));
?>

      

0


source


My first wish was to solve the problem with array_walk () and callback like this:

<?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 "

      

Live demo: http://3v4l.org/6B0WX

While this solves the problem without a foreach loop, it answers the what, not yes / no. The following code directly answers the question and allows you to answer the "what" as follows:

<?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: > >=

      

Live demo: http://3v4l.org/WJQ5c

If the answer was no, then the output is "No" followed by a space.

The key difference in this second solution versus the first is that in this case there is a return result that can be manipulated. If an element in the array matches a character in the string, then array_filter adds that element to $ chars. The new count of the array elements answers the question, and the array itself contains any matches if they want to display them.

0


source







All Articles