<?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.similar-text.php',
    1 => 'similar_text',
    2 => 'Calculate the similarity between two strings',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Functions',
  ),
  'prev' => 
  array (
    0 => 'function.sha1-file.php',
    1 => 'sha1_file',
  ),
  'next' => 
  array (
    0 => 'function.soundex.php',
    1 => 'soundex',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/similar-text.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.similar-text" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">similar_text</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">similar_text</span> &mdash; <span class="dc-title">Calculate the similarity between two strings</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.similar-text-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>similar_text</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string1</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string2</code></span>, <span class="methodparam"><span class="type"><a href="language.types.float.php" class="type float">float</a></span> <code class="parameter reference">&$percent</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   This calculates the similarity between two strings as described in
   Programming Classics: Implementing the World&#039;s Best Algorithms by Oliver (ISBN 0-131-00413-1). Note that this implementation does not use a
   stack as in Oliver&#039;s pseudo code, but recursive calls which may or may not
   speed up the whole process. Note also that the complexity of this algorithm
   is O(N**3) where N is the length of the longest string.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.similar-text-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string1</code></dt>
     <dd>
      <p class="para">
       The first string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">string2</code></dt>
     <dd>
      <p class="para">
       The second string.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        Swapping the <code class="parameter">string1</code> and
        <code class="parameter">string2</code> may yield a different result; see the
        example below.
       </p>
      </p></blockquote> 
     </dd>
    
    
     <dt><code class="parameter">percent</code></dt>
     <dd>
      <p class="para">
       By passing a reference as third argument,
       <span class="function"><strong>similar_text()</strong></span> will calculate the similarity in
       percent, by dividing the result of <span class="function"><strong>similar_text()</strong></span> by
       the average of the lengths of the given strings times
       <code class="literal">100</code>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.similar-text-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the number of matching chars in both strings.
  </p>
  <p class="para">
   The number of matching characters is calculated by finding the longest first
   common substring, and then doing this for the prefixes and the suffixes,
   recursively. The lengths of all found common substrings are added.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.similar-text-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="similar_text.example.swapping">
   <p><strong>Example #1 <span class="function"><strong>similar_text()</strong></span> argument swapping example</strong></p>
   <div class="example-contents"><p>
    This example shows that swapping the <code class="parameter">string1</code> and
    <code class="parameter">string2</code> argument may yield different results.
   </p></div>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$sim </span><span style="color: #007700">= </span><span style="color: #0000BB">similar_text</span><span style="color: #007700">(</span><span style="color: #DD0000">'bafoobar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'barfoo'</span><span style="color: #007700">, </span><span style="color: #0000BB">$perc</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"similarity: </span><span style="color: #0000BB">$sim</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$perc</span><span style="color: #DD0000"> %)\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sim </span><span style="color: #007700">= </span><span style="color: #0000BB">similar_text</span><span style="color: #007700">(</span><span style="color: #DD0000">'barfoo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bafoobar'</span><span style="color: #007700">, </span><span style="color: #0000BB">$perc</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"similarity: </span><span style="color: #0000BB">$sim</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$perc</span><span style="color: #DD0000"> %)\n"</span><span style="color: #007700">;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">similarity: 5 (71.428571428571 %)
similarity: 3 (42.857142857143 %)</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.similar-text-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.levenshtein.php" class="function" rel="rdfs-seeAlso">levenshtein()</a> - Calculate Levenshtein distance 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>
    <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>
   </ul>
  </p>
 </div>


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