Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 3989

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Calculate Levenshtein distance between two strings</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.lcfirst.html">lcfirst</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.localeconv.html">localeconv</a></div>
 <div class="up"><a href="ref.strings.html">String Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><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)</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="type">int</span> <span class="methodname"><strong>levenshtein</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str1</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$str2</code></span>
   )</div>

  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>levenshtein</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str1</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$str2</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$cost_ins</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$cost_rep</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$cost_del</code></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
   <em><code class="parameter">str1</code></em> into <em><code class="parameter">str2</code></em>.
   The complexity of the algorithm is <em>O(m*n)</em>,
   where <em>n</em> and <em>m</em> are the
   length of <em><code class="parameter">str1</code></em> and
   <em><code class="parameter">str2</code></em> (rather good when compared to
    <span class="function"><a href="function.similar-text.html" class="function">similar_text()</a></span>, which is O(max(n,m)**3),
   but still expensive).
  </p>
  <p class="para">
   In its simplest form the function will take only the two
   strings as parameter and will calculate just the number of
   insert, replace and delete operations needed to transform
   <em><code class="parameter">str1</code></em> into <em><code class="parameter">str2</code></em>.
  </p>
  <p class="para">
   A second variant will take three additional parameters that
   define the cost of insert, replace and delete operations.  This
   is more general and adaptive than variant one, but not as
   efficient.
  </p>
 </div>


<div class="refsect1 parameters" id="refsect1-function.levenshtein-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">str1</code></em></span>
     <dd>

      <p class="para">
       One of the strings being evaluated for Levenshtein distance.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">str2</code></em></span>
     <dd>

      <p class="para">
       One of the strings being evaluated for Levenshtein distance.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">cost_ins</code></em></span>
     <dd>

      <p class="para">
       Defines the cost of insertion.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">cost_rep</code></em></span>
     <dd>

      <p class="para">
       Defines the cost of replacement.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">cost_del</code></em></span>
     <dd>

      <p class="para">
       Defines the cost of deletion.
      </p>
     </dd>

    </dt>

   </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 or -1, if one of the argument strings
   is longer than the limit of 255 characters.
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.levenshtein-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4842">
    <p><strong>Example #1  <span class="function"><strong>levenshtein()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;input&nbsp;misspelled&nbsp;word<br /></span><span style="color: #0000BB">$input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'carrrot'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;array&nbsp;of&nbsp;words&nbsp;to&nbsp;check&nbsp;against<br /></span><span style="color: #0000BB">$words&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">//&nbsp;no&nbsp;shortest&nbsp;distance&nbsp;found,&nbsp;yet<br /></span><span style="color: #0000BB">$shortest&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;loop&nbsp;through&nbsp;words&nbsp;to&nbsp;find&nbsp;the&nbsp;closest<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$words&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$word</span><span style="color: #007700">)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;calculate&nbsp;the&nbsp;distance&nbsp;between&nbsp;the&nbsp;input&nbsp;word,<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;and&nbsp;the&nbsp;current&nbsp;word<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$lev&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">levenshtein</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$word</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;check&nbsp;for&nbsp;an&nbsp;exact&nbsp;match<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$lev&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;closest&nbsp;word&nbsp;is&nbsp;this&nbsp;one&nbsp;(exact&nbsp;match)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$closest&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$word</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$shortest&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;break&nbsp;out&nbsp;of&nbsp;the&nbsp;loop;&nbsp;we've&nbsp;found&nbsp;an&nbsp;exact&nbsp;match<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;if&nbsp;this&nbsp;distance&nbsp;is&nbsp;less&nbsp;than&nbsp;the&nbsp;next&nbsp;found&nbsp;shortest<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;distance,&nbsp;OR&nbsp;if&nbsp;a&nbsp;next&nbsp;shortest&nbsp;word&nbsp;has&nbsp;not&nbsp;yet&nbsp;been&nbsp;found<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$lev&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">$shortest&nbsp;</span><span style="color: #007700">||&nbsp;</span><span style="color: #0000BB">$shortest&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;set&nbsp;the&nbsp;closest&nbsp;match,&nbsp;and&nbsp;shortest&nbsp;distance<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$closest&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$word</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$shortest&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$lev</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Input&nbsp;word:&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">$shortest&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Exact&nbsp;match&nbsp;found:&nbsp;</span><span style="color: #0000BB">$closest</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Did&nbsp;you&nbsp;mean:&nbsp;</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="cdata"><pre>
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 class="member"> <span class="function"><a href="function.soundex.html" class="function" rel="rdfs-seeAlso">soundex()</a> - Calculate the soundex key of a string</span></li>
    <li class="member"> <span class="function"><a href="function.similar-text.html" class="function" rel="rdfs-seeAlso">similar_text()</a> - Calculate the similarity between two strings</span></li>
    <li class="member"> <span class="function"><a href="function.metaphone.html" class="function" rel="rdfs-seeAlso">metaphone()</a> - Calculate the metaphone key of a string</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.lcfirst.html">lcfirst</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.localeconv.html">localeconv</a></div>
 <div class="up"><a href="ref.strings.html">String Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>