Search code examples
phpfuzzy-search

fuzzy searching an array in php


after i searched i found how to do a fuzzy searching on a string

but i have an array of strings

$search = {"a" => "laptop","b" => "screen" ....}

that i retrieved from the DB MySQL

IS there any php class or function that does fuzzy searching on an array of words

or at least a link with maybe some useful info's

i saw a comment that recommend using PostgreSQL

and it's fuzzy searching capability but

the company had already a MySQL DB

Is there any recommendation ??


Solution

  • Look at the Levenshtein function

    Basically it gives you the difference (in terms of cost) between to strings. I.e. what is the cost to transform string A into string B.

    Set yourself a threshold levenshein distance and anything under that for two words mean they're similar.

    Also the Bitap algorithm is faster since it can be implemented via bitwise operators, but I believe you will have to implement it yourself, unless there is a PHP lib for it somewhere.

    EDIT To use levenshtein method:

    The search string is "maptop", and you set your "cost threshold" to say 2. That means you want any words that are two string transform operations away from your search string.

    so you loop through your array "A" of strings until

    levenshtein ( A[i] , searchString ) <= 2

    That will be your match. However you may get more than one word that matches, so it is up to you how you want to handle the extra results.