Henrikh
Henrikh

Reputation: 168

php array alike search

$array = array('hey','hi','hello');

if (in_array('hei',$array)) echo 'aaa';

This will not work, because there is no "hei" in the array, but I need something to search not with exact string.

In mySQL we have WHERE x ALIKE y. What can we use to find for alike string in array, not exact?

Thanks

Upvotes: 0

Views: 92

Answers (1)

Leonardo Delfino
Leonardo Delfino

Reputation: 1498

Use this function:

    function like_in_array( $sNeedle , $aHaystack )
    {

        foreach ($aHaystack as $sKey)
        {
            if( stripos( strtolower($sKey) , strtolower($sNeedle) ) !== false )
            {
                return true;
            }
        }

        return false;
    }

   if(like_in_array('hei', $array)) {
      echo 'in array';
   }

Upvotes: 2

Related Questions