//searchSuggestion.php
[code]
<?php

/**
 * @author Nico Kaag
 * @copyright 2009
 */
error_reporting(E_ALL);

/**
 * Give a list of search suggestions for a speciffic key word
 * @param string $keyword The keyword to search for
 * @param string $table The table in wich to search
 * @param string $field The field in wich to search
 * @param int $maxDistance The maximum difference between the keyword and suggestion
**/
function searchSuggestion($keyword, $table, $field, $maxDistance = 5)
{
	//Variable to store the closest match in
	//distance is -1 because it can't be an exact match, and it can be a possitive match
	$closestMatch = array('tag' => '', 'distance' => '-1');
	$return = array();
	$testKeyword = strtolower($keyword);
	$query = "SELECT ". $field ." FROM ". $table;
	$result = mysql_query($query);
	if (!$result || mysql_num_rows($result) == 0)
	{
		return false;
	}
	
	else
	{
		while ($row = mysql_fetch_assoc($result))
		{
			$tag = $row[$field];
			$testTag = strtolower($row[$field]);
		    // calculate the distance between the input word,
		    // and the current word
		    $levenstheinDistance = levenshtein($testKeyword, $testTag, 1, 2, 1);
		    //$levenstheinRatio = max(array(strlen($testTag), strlen($testKeyword))) / $levenstheinDistance;
		
		    // if this distance is less than the next found shortest
		    // distance, OR if a next shortest word has not yet been found
		    if ($levenstheinDistance <= $closestMatch['distance'] || $closestMatch['distance'] < 0 && $levenstheinDistance <= $maxDistance) {
		        // set the closest match, and shortest distance
		        $closestMatch['tag']  = $tag;
		        $closestMatch['distance'] = $levenstheinDistance;
		    }
		}
		
		//If a tag is found, return the array with the closest match
		if (!empty($closestMatch['tag']))
		{
			return $closestMatch['tag'];
		}
		
		//If no match is found, return false
		else
		{
			return false;
		}
	}
}

?>
[/code]

//index.php
[code]
<?php

/**
 * @author Nico Kaag
 * @copyright 2009
 */
error_reporting(E_ALL);

include 'config.php';
include 'searchSuggestion.php';
?>

<form method="post" action="index.php">
Zoek: <input type="text" name="searchWord"><br />
<input type="submit" value="Zoeken!">
</form>

<br /><br />
<?php
if (isset($_POST['searchWord']))
{
	$keyword = mysql_real_escape_string($_POST['searchWord']);
	$query = "SELECT tag FROM tags WHERE tag = '". $keyword ."' LIMIT 0,1";
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 1)
	{
		$row = mysql_fetch_assoc($result);
		echo 'Ik vond: '. $row['tag'];
	}
	else
	{
		$closestMatch = searchSuggestion($keyword, "tags", "tag");
		
		if (!$closestMatch)
		{
			echo 'Ik kon helaas niks voor u vinden.';
		}
		else
		{
			echo 'Zoekt u soms: <b>'. $closestMatch .'</b>?';
		}
	}
}

[/code]