2

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!

2
  • 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. Commented Apr 7, 2013 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. Commented Apr 7, 2013 at 22:13

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Just so I know, what does the offset option do?
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.
1

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);
}

Comments

1

how about:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.