Similar text PHP

29.Sep.2010

With PHP function similar_text(), you can offer your users a Google like “Did you mean” suggestion to a misspelled word. All you need is a word by word comparison using a PHP function similar_text. See and use snippet below in order to find most similar word to the one you choose.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$my_word = 'hallo';
 
$all_words = array(
	'hello',
	'hi',
	'PHP',
	'similar_text',
	'function'
);
 
$bestMatch = array('word' => $my_word, 'match' => 0);
 
foreach($all_words as $word) {
	similar_text($word, $my_word, $percent);
	if($percent > $bestMatch['match']) $bestMatch = array('word' => $word, 'match' => $percent);
}
 
if($bestMatch['match'] < 100) echo 'Did you mean: <strong>' . $bestMatch['word'] . '</strong>.';