将最近的字符串与特定字符串匹配

Match nearest string to a particular string

当用户搜索书名时,我应该使用什么搜索算法来给出结果?

如果存在一本书,则应返回该书名,否则必须显示与其接近的书名。

第一种情况比较简单,只需要搜索一个字符串。我需要如何解决大多数网站使用的第二种情况。

书籍 table 包含字段类别、class 和标题。我已经尝试了下面的代码,当我给出准确的标题时我得到了结果我希望系统给出密切相关的标题。

$title=isset($_GET['title'])?$_GET['title']:NULL;
      //$code=isset($_GET['code'])?$_GET['code']:NULL;
      $class=isset($_GET['class'])?$_GET['class']:NULL;
      $cat=isset($_GET['category'])?$_GET['category']:NULL;
      if(isset($title))
      $qry="select * from books where quantity>0 and title='$title' ";
      else{
          $qry="select * from books where quantity>0 ";
          if(isset($class)) $qry.=" and class='$class' ";
          if(isset($cat))   $qry.=" and category='$cat' ";
      }

嗯,首先我建议使用准备好的语句来避免通过您当前的代码进行 SQL 注入,但是在 SQL 中使用 LIKE 运算符应该在第二种情况下有效。

改变

`select * from books where quantity > 0 and title = '$title'`

`select * from books where quantity > 0 and title LIKE '%$title%'`

它的作用是检查单词周围是否有缺失的字母。因此,如果标题等于 "rabbit",则 "White rabbit"、"The Rabbit" 和 "Rabbit" 都将是结果。

p.s。空白不花钱