PHP Levenshtein for substr search


Keywords:php 


Question: 

I have a array with strings and need to search that contains some input string. But input can have mistype and contains several words. Search like a google.

Example:

$array = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit", 
          "Praesent elementum mattis risus at condimentum"];
$input = "dolor lrem";

1st string is ok. 2nd not.

levenshtein() is good for compare mistype strings, but its not suit for substr compare.


1 Answer: 

You could try something like this to calculate strings that match at an accuracy of 40% or greater. similar_text will return the number of matching characters in percent for you.

$array = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit", 
"Praesent elementum mattis risus at condimentum","It was popularised","and a search","There are many variations",
"dolor Ipsum is not lrem", "dolor", "lrem", "dolor lrem"];

$text = "dolor lrem";

$strings = array_filter($array, function ($input) use ($text) {
    // Your string accuracy algorithm
    return ((similar_text($text, $input, $percent) * 10) >= 40);
});

print_r($strings);

And that will return an array like this:

Array
(
[0] => Lorem ipsum dolor sit amet, consectetur adipiscing elit
[5] => dolor Ipsum is not lrem
[6] => dolor
[7] => lrem
[8] => dolor lrem
)