📄 function.levenshtein.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Calculate Levenshtein distance between two strings</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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 >= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">levenshtein</span> — <span class="dc-title">Calculate Levenshtein distance between two strings</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>levenshtein</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str1</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str2</tt></span> )</div> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>levenshtein</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str1</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str2</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$cost_ins</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$cost_rep</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$cost_del</tt></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 <i><tt class="parameter">str1</tt></i> into <i><tt class="parameter">str2</tt></i>. The complexity of the algorithm is <i>O(m*n)</i>, where <i>n</i> and <i>m</i> are the length of <i><tt class="parameter">str1</tt></i> and <i><tt class="parameter">str2</tt></i> (rather good when compared to <a href="function.similar-text.html" class="function">similar_text()</a>, 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 <i><tt class="parameter">str1</tt></i> into <i><tt class="parameter">str2</tt></i>. </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"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">str1</tt></i></span> <dd> <p class="para"> One of the strings being evaluated for Levenshtein distance. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">str2</tt></i></span> <dd> <p class="para"> One of the strings being evaluated for Levenshtein distance. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">cost_ins</tt></i></span> <dd> <p class="para"> Defines the cost of insertion. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">cost_rep</tt></i></span> <dd> <p class="para"> Defines the cost of replacement. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">cost_del</tt></i></span> <dd> <p class="para"> Defines the cost of deletion. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 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"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>levenshtein()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?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"><= </span><span style="color: #0000BB">$shortest </span><span style="color: #007700">|| </span><span style="color: #0000BB">$shortest </span><span style="color: #007700">< </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: $input\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: $closest\n"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"Did you mean: $closest?\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>Input word: carrrotDid you mean: carrot?</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.soundex.html" class="function" rel="rdfs-seeAlso">soundex()</a></li> <li class="member"><a href="function.similar-text.html" class="function" rel="rdfs-seeAlso">similar_text()</a></li> <li class="member"><a href="function.metaphone.html" class="function" rel="rdfs-seeAlso">metaphone()</a></li> </ul> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -