类似文本php mysql

类似文本php mysql,php,mysql,foreach,Php,Mysql,Foreach,我有下面的代码,当我搜索一个单词时,它会向我显示一个你的意思是什么。问题是如果我输入“clin”,我希望它返回临床医生,但它返回“reschedule” $my_word=$\u POST['value']; $bestMatch=array('word'=>$my_-word,'match'=>2); $result=mysql_查询(“从雅典娜选择关键字”); $storeArray=Array(); 而($row=mysql\u fetch\u array($result,mysql\u

我有下面的代码,当我搜索一个单词时,它会向我显示一个你的意思是什么。问题是如果我输入“clin”,我希望它返回临床医生,但它返回“reschedule”

$my_word=$\u POST['value'];
$bestMatch=array('word'=>$my_-word,'match'=>2);
$result=mysql_查询(“从雅典娜选择关键字”);
$storeArray=Array();
而($row=mysql\u fetch\u array($result,mysql\u ASSOC)){
相似的文本($row['keyword'],$myword,$percent);
如果($percent>$bestMatch['match']))
$bestMatch=array('word'=>$row['keyword'],'match'=>$percent);
}
如果($bestMatch['match']<70)
echo“你的意思是:。$bestMatch['word']。”;

我不熟悉PHP的
类似单词()
,但您也可以尝试以下方法:


这是一种用于执行所需搜索类型的流行算法。

我刚刚用一小组测试条目尝试了您的代码,效果很好。也许你从你的查询中得到了奇怪的结果?代码如下:

$my_word = $_REQUEST['value'];
$bestMatch = array('word' => $my_word, 'match' => 2);
$result = array("exam", "clinicals", "templates", "reschedule", "crafting", "php", "answer");
$storeArray = Array();
foreach ($result as $keyword) {
    similar_text($keyword, $my_word, $percent);
    if ($percent > $bestMatch['match'])
       $bestMatch = array('word' => $keyword, 'match' => $percent);
}
if ($bestMatch['match'] < 70)
   echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> p:'.$bestMatch['match'];

如果最后一个应该是
<70
<70
的话,它是在
while
语句的内部还是外部?它在while语句的外部,就像我刚才输入了“temp”,它的输出是指“考试”…模板在我的关键字中。。。UGH您使用的是什么版本的PHP?您可以尝试限制您的关键字池,然后回显每个比较,或者构建一个完整的比较数组(每个返回行一个),并验证百分比是否正常?我如何使用此lol来排列我的数据库。。由于某种原因,我一直收到一个错误。我的代码对你有用吗?试试你的版本,打印出你从数据库中得到的关键词。
$my_word = $_REQUEST['value'];
$bestMatch = array('word' => $my_word, 'match' => 2);
$result = array("exam", "clinicals", "templates", "reschedule", "crafting", "php", "answer");
$storeArray = Array();
foreach ($result as $keyword) {
    similar_text($keyword, $my_word, $percent);
    if ($percent > $bestMatch['match'])
       $bestMatch = array('word' => $keyword, 'match' => $percent);
}
if ($bestMatch['match'] < 70)
   echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> p:'.$bestMatch['match'];
$my_word = $_REQUEST['value'];
$bestMatch = array('word' => $my_word, 'match' => 2);
$result = array("exam", "clinicals", "templates", "reschedule", "crafting", "php", "answer");
$storeArray = Array();
foreach ($result as $keyword) {
    $lev = levenshtein ($keyword, $my_word, 1, 2, 1);
    if (!isset($lowest) || $lev < $lowest) {
       $bestMatch = array('word' => $keyword, 'match' => $lev);
       $lowest = $lev;
    }
}
if ($bestMatch['match'] > 0)
   echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> l:'.$bestMatch['match'];