• Time to read less than 1 minute

Keyword searching in PHP

If you're working on some PHP search engine logic, there is a handy - but not so known -  native function called similar_text() that can be of great help. The similar_text() function will calculate the similarity between 2 strings:

<?php
// Words to compare
$word1 = 'geography';
$word2 = 'discography';

// Calculate the score
$score = similar_text($word1, $word2);

// Outputs int(7)
var_dump($score);

Using this, simple logic, you can pass an array of keywords to your search logic, calculate the score of each keyword, and return results sorted by relevance (score).

Note that the similar_text() can take a third argument passed by reference that will provide a score percentage.

<?php
// Words to compare
$word1 = 'geography';
$word2 = 'discography';

// Calculate the score with a $percentage reference
$score = similar_text($word1, $word2, $percentage);

// Outputs int(7)
var_dump($score);

// Outputs int(70)
var_dump($percentage);

Another approach for keyword relevance calculation is the native levenshtein() function. This function returns the Levenshtein distance between two strings.

The Levenshtein distance is the number of characters you have to replace, insert or delete to transform a string into another string. The levenshtein() function is faster than the similar_text() function, but similar_text() will provide more accurate result with less modifications needed.