I have a PHP text array, which holds values like "Blue Pencil, Blue Pen, Blue, Red Pencil, Red Ink, Red Pen, Blue Notebook, etc....
"
I need to run through each array item, and show the matching results in order of matching RELEVANCE.
Like, if the user searches for the term "Blue
", then the 3rd item "Blue" which is a perfect match should get listed at the top, followed by 2nd item "Blue Pen", then by 1st "Blue Pencil" and finally by "Blue Notebook". Rest all non-Blue items will be discarded.
I tried using the sort
and rsort
functions on PHP arrays (both before and after pulling matching Blue items), but they simply sort based on alphabetical and reverse-alpha listing. There is no relevance match in there.
Like using sort($array) returns the following
Blue
Blue Notebook
Blue Pen
Blue Pencil
which is NOT really as per the expected "relevant" result.
Also the levenshtein
function does NOT fit, as it has a restriction that it works on strings with maximum length 255. My strings can be longer.
To draw a parallel, MySQL has this match-against clause which does the work.
SELECT * , MATCH (col1, col2) AGAINST ('some words' IN NATURAL LANGUAGE MODE)
Looking for something similar in PHP, if anyone can provide any pointers or any UDF to be written.