0
$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

Henrikh
  • 150
  • 2
  • 3
  • 11
  • You'd have to loop through the array checking each value with [soundex()](http://www.php.net/manual/en/function.soundex.php) or [levenshtein()](http://www.php.net/manual/en/function.levenshtein.php) or [similar_text()](http://www.php.net/manual/en/function.similar-text.php) to find similar words – Mark Baker Mar 06 '14 at 20:26
  • Possible duplicate: http://stackoverflow.com/q/5808923/3315914 – rpax Mar 06 '14 at 20:26

1 Answers1

2

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';
   }
Leonardo Delfino
  • 1,488
  • 10
  • 20