Marc Sanders
Marc Sanders

Reputation: 392

php check string for multiple values

I am trying to build a function that I can use to to check a string for multiple values, kind of a generic find needle in haystack kind of function. I have split the values up into an array and tried to loop through the array and check the value against the string with a for each loop but am not experiencing the expected outcome. Please see below the function, some examples and expected outcome.

Function

function find($haystack, $needle) {
    $needle = strtolower($needle);
    $needles = array_map('trim', explode(",", $needle));

    foreach ($needles as $needle) {
        if (strpos($haystack, $needle) !== false) {
            return true;
        }
    }

    return false;
}

Example 1

$type = 'dynamic'; // on a dynamic page, could be static, general, section, home on other pages depending on page and section

if (find($type, 'static, dynamic')) {
    // do something
} else {
    // do something
}

Outcome

This should catch the condition whether the $type contains static or dynamic and run the same code depending on page.

Example 2

$section = 'products labels'; // could contain various strings generated by site depending on page and section

if (find($section, 'products')) {
    // do something
} elseif (find($section, 'news')) {
    // do something
} else {
    // do something
}

Outcome

This should catch the condition specifically if the $section contains 'products' on page within the products section 'news' on a page within the news section.

--

Doesn't seem reliable on returning the desired results and can't figure out why! Any help greatly appreciated!

Upvotes: 2

Views: 3972

Answers (3)

Gerard Wunsch
Gerard Wunsch

Reputation: 11

how about:

str_ireplace($needles, '', $haystack) !== $haystack;

Upvotes: 1

Baba
Baba

Reputation: 95121

Why here is a 2way find that can come in handy

var_dump(find('dynamic', 'static, dynamic')); // expect true
var_dump(find('products labels', 'products')); // expect true
var_dump(find('foo', 'food foor oof')); // expect false

Function used

function find($str1, $str2, $tokens = array(" ",",",";"), $sep = "~#") {
    $str1 = array_filter(explode($sep, str_replace($tokens, $sep, strtolower($str1))));
    $str2 = array_filter(explode($sep, str_replace($tokens, $sep, strtolower($str2))));
    return array_intersect($str1, $str2) || array_intersect($str2, $str1);
}

Upvotes: 1

Kevin Lynch
Kevin Lynch

Reputation: 24713

Something like this maybe

function strposa($haystack, $needles=array(), $offset=0) {
    $chr = array();
    foreach($needles as $needle) {
            $res = strpos($haystack, $needle, $offset);
            if ($res !== false) $chr[$needle] = $res;
    }
    if(empty($chr)) return false;
    return min($chr);
}

and then

$string = 'Whis string contains word "cheese" and "tea".';
$array  = array('burger', 'melon', 'cheese', 'milk');

if (strposa($string, $array, 1)) {
    echo 'true';
} else {
    echo 'false';
}

This will be true because of cheese

Upvotes: 3

Related Questions