我正在尝试将用户提交的字符串与数据库记录的字符串进行比较,并查看它们与%
的接近程度我发现这个相当有趣的代码看起来像一个很好的解决方案
Function Compare(ByVal str1 As String, ByVal str2 As String) As Double
Dim count As Integer = If(str1.Length > str2.Length, str1.Length, str2.Length)
Dim hits As Integer = 0
Dim i, j As Integer : i = 0 : j = 0
For i = 0 To str1.Length - 1
If str1.Chars(i) = " " Then i += 1 : j = str2.IndexOf(" "c, j) + 1 : hits += 1
While j < str2.Length AndAlso str2.Chars(j) <> " "c
If str1.Chars(i) = str2.Chars(j) Then
hits += 1
j += 1
Exit While
Else
j += 1
End If
End While
If Not (j < str2.Length AndAlso str2.Chars(j) <> " "c) Then
j -= 1
End If
Next
Return Math.Round((hits / count), 2)
End Function
首先,任何人都可以告诉我上面使用的是什么语言,有人可以帮我转换成php吗?
我试图转换它,但在早期遇到了一些麻烦
function compare($str1,$str2) as $double
{
$count = if(strlen($str1) > strlen($str2), strlen($str1) > strlen($str2));
$hits = 0;
$i - 0;
$j = 0;
for($i = 0; $i < strlen($str1); $i++)
{
if($str1[$i] == " ")
{
$i .= "1";
}
}
}
对此的任何帮助都将非常感激
答案 0 :(得分:2)
作为一种选择,然后尝试这样的事情:
$teststr = "This is a test.";
$dbstr = "This was a test.";
$percent = (1 - levenshtein($teststr, $dbstr)/max( strlen($teststr),strlen($dbstr) ) ) * 100;
print "Percent match".$percent."\n";
Percent match: 92.857142857143