38
wmatch
wmatch
SYNOPSIS
wmatch($word, $wl, $dlimit=0, $closest=true)
DESCRIPTION
wmatch searches the list of words $wl for the words matching $word.
A match is determined from the Levenshtein distance. If $closest is true, only the closest matching words are returned.
wmatch is case-insensitive and accent-insensitive.
IMPORTANT: The global variables $search_distance and $search_closest defined in config.inc control the behavior of wmatch.
$search_distance specifies the maximum Levenshtein distance accepted for a match. A value of 0 requires an exact match, 1 accepts close matches, 2 less close matches, and so on.
If $search_closest is true, only the closest matches are returned. Otherwise, all matches within the specified distance are returned.
EXAMPLE
php > require 'library/wmatch.php';
php > $words = array('orange', 'orage', 'orageux', 'oragee');
php > var_dump(wmatch('orangé', $words, 2));
array(1) {
[0]=>
string(6) "orange"
}
CODE
- require_once 'strflat.php';
- function wmatch($word, $wl, $dlimit=0, $closest=true) {
- $word = strtolower(strflat($word));
- $ret = false;
- foreach ($wl as $w) {
- $d = levenshtein($word, strtolower(strflat($w)));
- if ($d < 0) {
- continue;
- }
- /* DON'T return immediately if $d is 0 to be case and accent insensitive */
- if ($d <= $dlimit) {
- if ($closest && $d < $dlimit) {
- $ret=array($w);
- $dlimit=$d;
- }
- else {
- if ($ret === false)
- $ret=array();
- $ret[]=$w;
- }
- }
- }
- return $ret;
- }
Comments