Search for a string or part of string in PHP

╄→尐↘猪︶ㄣ 提交于 2019-11-28 00:43:53
Marc B

array_filter lets you specify a custom function to do the searching. In your case, a simple function that uses strpos() to check if your search string is present:

function my_search($haystack) {
    $needle = 'value to search for';
    return(strpos($haystack, $needle)); // or stripos() if you want case-insensitive searching.
}

$matches = array_filter($your_array, 'my_search');

Alternatively, you could use an anonymous function to help prevent namespace contamination:

$matches = array_filter($your_array, function ($haystack) use ($needle) {
    return(strpos($haystack, $needle));
});
foreach($array as $item){
  if(strpos($item,"mysearchword")!== false){
    echo 'found';
  }
}

or you can use preg_match for more flexible search instead of strpos.

Unfortunately, search is one of the more difficult things to do in computer science. If you build for search based on literal string matches or regular expressions (regex), you may find that you'll be unhappy with the relevance of the results that are returned.

If you're interested in rolling up your sleeves and getting a little dirty with a more sophisticated solution, I'd try Zend's Lucene implementation ( http://framework.zend.com/manual/en/zend.search.lucene.html ). I've implemented a search on a site with it. It took a few days, but the results were MUCH better than the 15 minute solution of literal string matching.

Let me know if you'd like more info.

Best of luck!

PS. Here's an example: http://devzone.zend.com/article/91

I think Marc B's answer was a good starting point but for me it had some problems. Such as you have to know what the Needle is at "compile time" because you can't dynamically change that value. also if the needle appeared at the start of the string element it would act like it's not there at all. so after a little experimenting I manged to come up with a way around both problems. so you don't have to create a new function for every different needle your going to want to use anymore.

function my_search($haystack)
{
    global $needle;
    if( strpos($haystack, $needle) === false) {return false;} else {return true;}
}

and it would be called like this:

$needle="item to search for";
$matches = array_filter($my_array, 'my_search');

and being as needle is now accessible in the same scope that the rest of the code is you can set needle to any other string variable you wanted, including user input.

You can user regular expressions on each and every array element like

foreach($array as $value)
{
    //... your search statement for $value
}

iterate through the array and use substr if you want partial matches:

http://php.net/manual/en/function.substr.php

this should be sufficient if you have a "small" store...

I have same Issue but i have created i function to search in array by passing the array, key and value.

public function searchinarr($array, $key, $value)
{
       $results = array();
            for($i=0;$i<count($array);$i++)
            {
                foreach($array[$i] as $k=>$val)
                {
                    if($k==$key)
                    {
                        if(strpos($val,$value)!== false)
                        {
                            $results[] = $array[$i];
                        }
                    }
                }

            }
            return $results;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!