我试图检查字符串的大致相似性。在PHP中检查两个字符串的近似匹配
这是我使用的标准。
1)单词的顺序很重要 2)单词可以有80%的相似性。
实施例:
$string1 = "How much will it cost to me" //string in vocabulary (all "right" words is here)
$string2 = "How much does costs it " //"costs" instead "cost" -is a deliberate mistake (user input);
Algoritm: 1)检查字的相似性,并创建清洁字符串“右”的话(根据它出现在词汇中的顺序)。 输出:“多少钱费用” 2)创建干净的字符串与“正确”的话,以便它出现在用户输入。 输出:“多少成本” 3)比较两个输出 - 如果不相同 - 返回否,否则如果相同返回是。
有什么建议吗?我开始编写代码,但我不熟悉PHP中的工具,所以我不知道如何理性和有效地做到这一点。
它看上去更像是使用Javascript/PHP的
$string1="how much will it cost for me" ;
$string2= "how much does costs it";
function compareStrings($string1, $string2) {
if (strlen($s1)==0 || strlen($s2)==0) {
return 0;
}
while (strpos($s1, " ")!==false) {
$s1 = str_replace(" ", " ", $s1);
}
while (strpos($s2, " ")!==false) {
$s2 = str_replace(" ", " ", $s2);
}
$ar1 = explode(" ",$s1);
$ar2 = explode(" ",$s2);
$array1 = array_flip($ar1);
$array2 = array_flip($ar2);
$l1 = count($ar1);
$l2 = count($ar2);
$meaning="";
$rightorder=""
for ($i=0;$i<=$l1;$i++) {
for ($j=0;$j<=$l2;$j++) {
$k= similar_text($array1[i], $array2[j], $perc).PHP_EOL;
if ($perc>=85) {
$meaning=$meaning." ".$array1[j]; //generating a string of the first output
$rightorder[i]= array1[i]; //generating the array with second output
}
}
}
}
的想法泰德的$意义将得到 “多少它的成本” 美元再经过rightorder将获得
$rightorder[0]='how'
$rightorder[1]='much'
$rightorder[2]=''
$rightorder[3]='cost'
$rightorder[4]='it'
我会以某种方式反转回字符串“多少成本”
并比较这两个。
if ("how much cost it"=="how much it cost") return true; else return false.
查看[levenshtein()](http://php.net/manual/en/function.levenshtein.php)和[similar_text()](http://www.php.net/manual/en /function.similar-text。PHP)提供的功能,它们可能适合账单。 – hexblot 2013-05-14 13:20:43
不确定... – 2013-05-14 13:23:20
另外[soundex](http://php.net/manual/en/function.soundex.php) – 2013-05-14 13:23:51