PHP: similar_text - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

soundex sha1

Last updated: Fri, 26 Jan 2007

view this page in

# similar_text

(PHP 3 >= 3.0.7, PHP 4, PHP 5)

similar_text -- 袙褘褔懈褋谢褟械褌 褋褌械锌械薪褜 锌芯褏芯卸械褋褌懈 写胁褍褏 褋褌褉芯泻

## 袨锌懈褋邪薪懈械

int similar_text ( string first, string second [, float percent] )

袙褘褔懈褋谢褟械褌 褋褌械锌械薪褜 锌芯褏芯卸械褋褌懈 写胁褍褏 褋褌褉芯泻 锌芯 邪谢谐芯褉懈褌屑褍, 芯锌懈褋邪薪薪芯屑褍 Oliver [1993]. 协褌邪 褉械邪谢懈蟹邪褑懈褟 邪谢谐芯褉懈褌屑邪 薪械 懈褋锌芯谢褜蟹褍械褌 褋褌褝泻邪, 懈褋锌芯谢褜蟹芯胁邪薪薪芯谐芯 胁 芯褉懈谐懈薪邪谢械, 胁屑械褋褌芯 褝褌芯谐芯 锌褉懈屑械薪褟褞褌褋褟 褉械泻褍褉褋懈胁薪褘械 胁褘蟹芯胁褘, 褔褌芯 胁 薪械泻芯褌芯褉褘褏 褋谢褍褔邪褟褏 屑芯卸械褌 褍褋泻芯褉懈褌褜 锌褉芯褑械褋褋. 小谢芯卸薪芯褋褌褜 邪谢谐芯褉懈褌屑邪 褋芯褋褌邪胁谢褟械褌 O(N**3), 谐写械 N - 写谢懈薪邪 斜芯谢械械 写谢懈薪薪芯泄 懈蟹 写胁褍褏 褋褌褉芯泻.

袩褉懈 锌械褉械写邪褔械 锌芯 褋褋褘谢泻械 褌褉械褌褜械谐芯 邪褉谐褍屑械薪褌邪, 械屑褍 锌褉懈褋胁邪懈胁邪械褌褋褟 褋褌械锌械薪褜 锌芯褏芯卸械褋褌懈 写胁褍褏 褋褌褉芯泻 胁 锌褉芯褑械薪褌邪褏. 袙芯蟹胁褉邪褖邪械褌褋褟 泻芯谢懈褔械褋褌胁芯 褋芯胁锌邪写邪褞褖懈褏 褋懈屑胁芯谢芯胁 胁 写胁褍褏 褋褌褉芯泻邪褏.

add a note User Contributed Notes
similar_text
Paul
18-Jan-2007 08:50
`The speed issues for similar_text seem to be only an issue for long sections of text (>20000 chars).
I found a huge performance improvement in my application by just testing if the string to be tested was less than 20000 chars before calling similar_text.
20000+ took 3-5 secs to process, anything else (10000 and below) took a fraction of a second.
Fortunately for me, there was only a handful of instances with >20000 chars which I couldn't get a comparison % for.`
dmitry dot polushkin at gmail dot com
07-Mar-2006 08:08
`Well... hard to explain, why I have written this function, but maybe it will be usefull.
<?php
// returns the percentage of the string "similarity"
function str_compare($str1, $str2) {
$count = 0;
$str1 = ereg_replace("[^a-z]", ' ', strtolower($str1));
while(strstr($str1, ' ')) {
$str1 = str_replace(' ', ' ', $str1);
}
$str1 = explode(' ', $str1);
$str2 = ereg_replace("[^a-z]", ' ', strtolower($str2));
while(strstr($str2, ' ')) {
$str2 = str_replace(' ', ' ', $str2);
}
$str2 = explode(' ', $str2);
if(count($str1)<count($str2)) {
$tmp = $str1;
$str1 = $str2;
$str2 = $tmp;
unset($tmp);
}
for($i=0; $i<count($str1); $i++) {
if(in_array($str1[$i], $str2)) {
$count++;
}
}
return $count/count($str2)*100;
}
?>`
brad dot fish at gmail dot com
24-Feb-2006 02:30
`The link below to the Oliver document appears to be broken. Here is one that works: http://citeseer.ist.psu.edu/oliver93decision.html`
louis #at# mulliemedia.com
06-Jun-2004 05:01
`Note that this function will calculate the percentage blindly, without regard to the LENGHT of the string.
This may become important if you try to print similar names to SMALL strings :
e.g.
I want to print out the value if it is 90 percent similar to the other one : the value is HE, the correct value is HEC
The similar_text() function will return approximately 66.7 %, and it will not print it because it is smaller than 90 %, although almost all of the string was matched.`
hate at spam dot com dot BR
09-May-2004 09:21
`In PHP4+, you don't need to pass the percent variable as reference..
Instead, use this way:
<?
similar_text($string1, $string2, $p);
echo "Percent: $p%";
?>
In PHP5, you'll get a ugly warning message when passing this variable as reference.. But it's configurable in php.ini (allow_call_time_pass_reference = Off)
That's it... Another great function! :)`
julius at infoguiden dot no
06-Feb-2003 06:46
`If you have reserved names in a database that you don't want others to use, i find this to work pretty good.
I added strtoupper to the variables to validate typing only. Taking case into consideration will decrease similarity.
$query = mysql_query("select * from $table") or die("Query failed");
while ($row = mysql_fetch_array($query)) {
similar_text(strtoupper($_POST['name']), strtoupper($row['reserved']), $similarity_pst);
if (number_format($similarity_pst, 0) > 90){
$too_similar = $row['reserved'];
print "The name you entered is too similar the reserved name &quot;".$row['reserved']."&quot;";
break;
}
}`
georgesk at hotmail dot com
08-Mar-2002 08:14
`Well, as mentioned above the speed is O(N^3), i've done a longest common subsequence way that is O(m.n) where m and n are the length of str1 and str2, the result is a percentage and it seems to be exactly the same as similar_text percentage but with better performance... here's the 3 functions i'm using..
function LCS_Length($s1, $s2)
{
$m = strlen($s1);
$n = strlen($s2);
//this table will be used to compute the LCS-Length, only 128 chars per string are considered
$LCS_Length_Table = array(array(128),array(128));
//reset the 2 cols in the table
for($i=1; $i < $m; $i++) $LCS_Length_Table[$i][0]=0;
for($j=0; $j < $n; $j++) $LCS_Length_Table[0][$j]=0;
for ($i=1; $i <= $m; $i++) {
for ($j=1; $j <= $n; $j++) {
if ($s1[$i-1]==$s2[$j-1])
$LCS_Length_Table[$i][$j] = $LCS_Length_Table[$i-1][$j-1] + 1;
else if ($LCS_Length_Table[$i-1][$j] >= $LCS_Length_Table[$i][$j-1])
$LCS_Length_Table[$i][$j] = $LCS_Length_Table[$i-1][$j];
else
$LCS_Length_Table[$i][$j] = $LCS_Length_Table[$i][$j-1];
}
}
return $LCS_Length_Table[$m][$n];
}
function str_lcsfix($s)
{
$s = str_replace(" ","",$s);
$s = ereg_replace("[殍觌耸扇]","e", $s);
$s = ereg_replace("[噌忏溴呐寐晾]","a", $s);
$s = ereg_replace("[祉铒衔吞]","i", $s);
$s = ereg_replace("[蝮趱鲋赵覿","o", $s);
$s = ereg_replace("[苒谫]","u", $s);
$s = ereg_replace("[荾","c", $s);
return $s;
}
function get_lcs($s1, $s2)
{
//ok, now replace all spaces with nothing
$s1 = strtolower(str_lcsfix($s1));
$s2 = strtolower(str_lcsfix($s2));
$lcs = LCS_Length($s1,$s2); //longest common sub sequence
$ms = (strlen($s1) + strlen($s2)) / 2;
return (($lcs*100)/$ms);
}
you can skip calling str_lcsfix if you don't worry about accentuated characters and things like that or you can add up to it or modify it for faster performance, i think ereg is not the fastest way?
hope this helps.
Georges`
daniel at reflexionsdesign dot com
09-Oct-2001 06:30
`If performance is an issue, you may wish to use the levenshtein() function instead, which has a considerably better complexity of O(str1 * str2).`
mogmios at hushmail dot com
01-Feb-2000 05:39
`$i = similar_text($first_word, $second_word, &$p);
echo("Matched: $i Percentage: $p%");
Don't forget your passing the double as a reference. If you use this and soundex() together you can get a pretty good guess as to how well two words match. Is useful for simple bot-like programs.`
add a note
soundex sha1

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST