Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 比较两个字符串的匹配百分比_Php - Fatal编程技术网

Php 比较两个字符串的匹配百分比

Php 比较两个字符串的匹配百分比,php,Php,我试图将用户提交的字符串与数据库记录的字符串进行比较,看看它们在%方面有多接近 我发现了这段相当有趣的代码,它看起来是一个很好的解决方案 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

我试图将用户提交的字符串与数据库记录的字符串进行比较,看看它们在%方面有多接近

我发现了这段相当有趣的代码,它看起来是一个很好的解决方案

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
函数比较(ByVal str1为字符串,ByVal str2为字符串)为双精度
整数Dim计数=If(str1.Length>str2.Length,str1.Length,str2.Length)
以整数=0的形式显示Dim hits
尺寸i,j为整数:i=0:j=0
对于i=0到str1。长度为-1
如果str1.Chars(i)=“”,那么i+=1:j=str2.IndexOf(““c,j)+1:hits+=1
而j
首先,有人能告诉我上面使用的是什么语言吗?有人能帮我把它转换成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";
    }
}
}
函数比较($str1,$str2)为$double
{
$count=if(strlen($str1)>strlen($str2),strlen($str1)>strlen($str2));
$hits=0;
$i-0;
$j=0;
对于($i=0;$i

如果您能提供任何帮助,我们将不胜感激。

请尝试以下方法:

$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

更多信息请访问:

您在哪里找到代码?也许这是第一条线索?你可以考虑使用VisualBasic。你是否考虑过使用<代码> int LVENHTETIN(String $STR1,String $STR2)来测量字符串的相似性?或者你必须使用上面的东西吗?看看这个,选择你最喜欢的:这是我用谷歌搜索找到的东西,网站是codeproject:)。。。我只是想得到最准确的结果和最后的百分比:)我在做数据库搜索,试图返回最接近的匹配,谢谢