Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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!

share|improve this question
1  
Your strtolower call ends up not doing anything due to a typo on the next line, but this code should still work as given -- at least for these two particular cases. –  Jon Apr 7 '13 at 20:52
    
Thanks for pointing that out, I have amended the function above. As you said I believe the function is working correctly, I may have got tangled up and made an error on my part on some of the if / elseif / else statements I was using within my code. –  Marc Sanders Apr 7 '13 at 22:13

3 Answers 3

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

share|improve this answer
    
Just so I know, what does the offset option do? –  Marc Sanders Apr 7 '13 at 21:09
    
The offset specifies at what point in the string you wish to start searching. In this case we wish to start searching from the beginning. –  Vector Apr 7 '13 at 21:14

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);
}
share|improve this answer

how about:

str_ireplace($needles, '', $haystack) !== $haystack;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.