PHP - 字符串包含

I have an array witch contains badge objects. I'm trying to remove objects that don't match search criteria, at the moment the criteria is if the name doesn't match a searched string

The code i have so far is

        foreach ( $badgeList as $key=>$badge ) {
            $check = strpos($badge->getName() , $_POST['name']);
            if($check === false) {
                unset($badgeList[$key]);
                print "<br/>" . $badge->getName() . " -- post: " . $_POST['name'];
            }
        }

Whats happening is its remove all objects from the array, even those that do match the string

This is whats being printed

Outdoor Challenge -- post: outdoor

Outdoor Plus Challenge -- post: outdoor

Outdoor Challenge -- post: outdoor

Outdoor Challenge -- post: outdoor

Nights Away 1 -- post: outdoor

Year 1 -- post: outdoor

Nights Away 5 -- post: outdoor

If you need looser matching, use a case insensitive function or regex:

stristr($badge->getName() , $_POST['name'])

or

if( ! preg_match("/" . $_POST['name'] . "/i",$badge->getName()) ) {

In these suggestions stristr is the case insensitive version of strstr and /i is the case insensitive flag for regex

One way of solving this would be as follows:

$check = strpos(strtolower($badge->getName()) , strtolower($_POST['name']));

strtolower() will convert both haystack and needle to lowercase strings and now your search for "string" will match "String" as well.

Your original issue is that strpos() does a case sensitive search ('S' is not the same as 's')