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
  1. require_once 'strflat.php';
  2.  
  3. function wmatch($word, $wl, $dlimit=0, $closest=true) {
  4.     $word = strtolower(strflat($word));
  5.     $ret = false;
  6.  
  7.     foreach ($wl as $w) {
  8.         $d = levenshtein($word, strtolower(strflat($w)));
  9.  
  10.         if ($d < 0) {
  11.             continue;
  12.         }
  13.  
  14.         /* DON'T return immediately if $d is 0 to be case and accent insensitive */
  15.  
  16.         if ($d <= $dlimit) {
  17.             if ($closest && $d < $dlimit) {
  18.                 $ret=array($w);
  19.                 $dlimit=$d;
  20.             }
  21.             else {
  22.                 if ($ret === false)
  23.                     $ret=array();
  24.                 $ret[]=$w;
  25.             }
  26.         }
  27.     }
  28.  
  29.     return $ret;
  30. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].