<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.levenshtein.php',
    1 => 'levenshtein',
    2 => 'Calculate Levenshtein distance between two strings',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Functions',
  ),
  'prev' => 
  array (
    0 => 'function.lcfirst.php',
    1 => 'lcfirst',
  ),
  'next' => 
  array (
    0 => 'function.localeconv.php',
    1 => 'localeconv',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/levenshtein.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.levenshtein" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">levenshtein</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">levenshtein</span> &mdash; <span class="dc-title">Calculate Levenshtein distance between two strings</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.levenshtein-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>levenshtein</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string1</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string2</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$insertion_cost</code><span class="initializer"> = 1</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$replacement_cost</code><span class="initializer"> = 1</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$deletion_cost</code><span class="initializer"> = 1</span></span><br>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   The Levenshtein distance is defined as the minimal number of
   characters you have to replace, insert or delete to transform
   <code class="parameter">string1</code> into <code class="parameter">string2</code>.
   The complexity of the algorithm is <code class="literal">O(m*n)</code>,
   where <code class="literal">n</code> and <code class="literal">m</code> are the
   length of <code class="parameter">string1</code> and
   <code class="parameter">string2</code> (rather good when compared to
   <span class="function"><a href="function.similar-text.php" class="function">similar_text()</a></span>, which is <code class="literal">O(max(n,m)**3)</code>,
   but still expensive).
  </p>
  <p class="para">
   If <code class="parameter">insertion_cost</code>, <code class="parameter">replacement_cost</code>
   and/or <code class="parameter">deletion_cost</code> are unequal to <code class="literal">1</code>,
   the algorithm adapts to choose the cheapest transforms.
   E.g. if <code class="code">$insertion_cost + $deletion_cost &lt; $replacement_cost</code>,
   no replacements will be done, but rather inserts and deletions instead.
  </p>
 </div>


<div class="refsect1 parameters" id="refsect1-function.levenshtein-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string1</code></dt>
     <dd>
      <p class="para">
       One of the strings being evaluated for Levenshtein distance.
      </p>
     </dd>
    
    
     <dt><code class="parameter">string2</code></dt>
     <dd>
      <p class="para">
       One of the strings being evaluated for Levenshtein distance.
      </p>
     </dd>
    
    
     <dt><code class="parameter">insertion_cost</code></dt>
     <dd>
      <p class="para">
       Defines the cost of insertion.
      </p>
     </dd>
    
    
     <dt><code class="parameter">replacement_cost</code></dt>
     <dd>
      <p class="para">
       Defines the cost of replacement.
      </p>
     </dd>
    
    
     <dt><code class="parameter">deletion_cost</code></dt>
     <dd>
      <p class="para">
       Defines the cost of deletion.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.levenshtein-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   This function returns the Levenshtein-Distance between the
   two argument strings.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.levenshtein-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       Prior to this version, <span class="function"><strong>levenshtein()</strong></span> had to be called
       with either two or five arguments.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Prior to this version, <span class="function"><strong>levenshtein()</strong></span> would return <code class="literal">-1</code>
       if one of the argument strings is longer than 255 characters.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.levenshtein-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>levenshtein()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// input misspelled word<br /></span><span style="color: #0000BB">$input </span><span style="color: #007700">= </span><span style="color: #DD0000">'carrrot'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// array of words to check against<br /></span><span style="color: #0000BB">$words  </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,</span><span style="color: #DD0000">'pineapple'</span><span style="color: #007700">,</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">,</span><span style="color: #DD0000">'orange'</span><span style="color: #007700">,<br />                </span><span style="color: #DD0000">'radish'</span><span style="color: #007700">,</span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">,</span><span style="color: #DD0000">'pea'</span><span style="color: #007700">,</span><span style="color: #DD0000">'bean'</span><span style="color: #007700">,</span><span style="color: #DD0000">'potato'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// no shortest distance found, yet<br /></span><span style="color: #0000BB">$shortest </span><span style="color: #007700">= -</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// loop through words to find the closest<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$words </span><span style="color: #007700">as </span><span style="color: #0000BB">$word</span><span style="color: #007700">) {<br /><br />    </span><span style="color: #FF8000">// calculate the distance between the input word,<br />    // and the current word<br />    </span><span style="color: #0000BB">$lev </span><span style="color: #007700">= </span><span style="color: #0000BB">levenshtein</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">$word</span><span style="color: #007700">);<br /><br />    </span><span style="color: #FF8000">// check for an exact match<br />    </span><span style="color: #007700">if (</span><span style="color: #0000BB">$lev </span><span style="color: #007700">== </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /><br />        </span><span style="color: #FF8000">// closest word is this one (exact match)<br />        </span><span style="color: #0000BB">$closest </span><span style="color: #007700">= </span><span style="color: #0000BB">$word</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$shortest </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br />        </span><span style="color: #FF8000">// break out of the loop; we've found an exact match<br />        </span><span style="color: #007700">break;<br />    }<br /><br />    </span><span style="color: #FF8000">// if this distance is less than the next found shortest<br />    // distance, OR if a next shortest word has not yet been found<br />    </span><span style="color: #007700">if (</span><span style="color: #0000BB">$lev </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">$shortest </span><span style="color: #007700">|| </span><span style="color: #0000BB">$shortest </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />        </span><span style="color: #FF8000">// set the closest match, and shortest distance<br />        </span><span style="color: #0000BB">$closest  </span><span style="color: #007700">= </span><span style="color: #0000BB">$word</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$shortest </span><span style="color: #007700">= </span><span style="color: #0000BB">$lev</span><span style="color: #007700">;<br />    }<br />}<br /><br />echo </span><span style="color: #DD0000">"Input word: </span><span style="color: #0000BB">$input</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">$shortest </span><span style="color: #007700">== </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"Exact match found: </span><span style="color: #0000BB">$closest</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />} else {<br />    echo </span><span style="color: #DD0000">"Did you mean: </span><span style="color: #0000BB">$closest</span><span style="color: #DD0000">?\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Input word: carrrot
Did you mean: carrot?</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.levenshtein-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.soundex.php" class="function" rel="rdfs-seeAlso">soundex()</a> - Calculate the soundex key of a string</span></li>
    <li><span class="function"><a href="function.similar-text.php" class="function" rel="rdfs-seeAlso">similar_text()</a> - Calculate the similarity between two strings</span></li>
    <li><span class="function"><a href="function.metaphone.php" class="function" rel="rdfs-seeAlso">metaphone()</a> - Calculate the metaphone key of a string</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>