【问题标题】:how to make case insensitive search如何进行不区分大小写的搜索
【发布时间】:2013-02-24 18:41:21
【问题描述】:

我正在使用 jQuery autocompleter 进行搜索。

从下面的代码中,我的搜索只发生在区分大小写的情况下,我想进行不区分大小写的搜索。 例如。 $array={the king, The king, The man}

我的搜索关键字是“The”,那么目前我只得到像 {The king, The man} 这样的输出 我没有收到the king,因为它是小写的。

我想要的是,如果我写“the”那么我应该得到所有三个结果。

请帮我解决我的问题

view.php

<script type="text/javascript">
$(document).ready(function() {
        $(function() {
                $( "#autocomplete" ).autocomplete({


                        source: function(request, response) {

                     $.ajax({ url: "<?php echo base_url().'search/suggestions'?>",
                                data: { term: $("#autocomplete").val()},
                                dataType: "json",
                                type: "POST",
                                success: function(data){

                                        response(data);
                                }
                        });
                },

                minLength: 3,
                select: function (a, b) {
    $(this).val(b.item.value);
    $(".searchform1").submit()
}
                });
        });
        $('#autocomplete').focus(); 
});


  function monkeyPatchAutocomplete() {

      // Don't really need to save the old fn, 
      // but I could chain if I wanted to
      var oldFn = $.ui.autocomplete.prototype._renderItem;

      $.ui.autocomplete.prototype._renderItem = function( ul, item) {
          var re = new RegExp("^" + this.term, "i") ;
          var t = item.label.replace(re,"<span style='font-weight:bold;color:Black;'>" + this.term + "</span>");
          return $( "<li></li>" )
              .data( "item.autocomplete", item )
              .append( "<a>" + t + "</a>" )
              .appendTo( ul );
      };
  }


  $(document).ready(function() {

      monkeyPatchAutocomplete();

      $("#input1").autocomplete({
          // The source option can be an array of terms.  In this case, if
          // the typed characters appear in any position in a term, then the
          // term is included in the autocomplete list.
          // The source option can also be a function that performs the search,
          // and calls a response function with the matched entries.
          source: function(req, responseFn) {
              addMessage("search on: '" + req.term + "'<br/>");
              var re = $.ui.autocomplete.escapeRegex(req.term);
              var matcher = new RegExp( "^" + re, "i" );
              var a = $.grep( wordlist, function(item,index){
                  //addMessage("&nbsp;&nbsp;sniffing: '" + item + "'<br/>");
                  return matcher.test(item);
              });
              addMessage("Result: " + a.length + " items<br/>");
              responseFn( a );
          },

          select: function(value, data){
              if (typeof data == "undefined") {
                  addMessage('You selected: ' + value + "<br/>");
              }else {
                  addMessage('You selected: ' + data.item.value + "<br/>");
              }
          }
      });
  });
</script>
     ---------------
     ---------------
        <form class="searchform1" action="<?php echo base_url().'search/searchresult/pgn/grid/'?>" method="POST"> 
                        <img src="<?php echo base_url()?>css/images/icons/searchicon.png" style="vertical-align:middle;"/>  
                        <input id="autocomplete" value="<?php  if(isset($srchvalue)){echo $srchvalue ;} ?>" class="deletable"   type="text" name="keywords" style="height: 30px;width: 450px;font-size: 16px;border: 0;border-bottom:solid 4px #dfdfdf;border-top:solid 4px #dfdfdf;"  maxlength="70" placeholder="  Search for Title, Author Name, Isbn , Publisher"/>

                        <input type="submit" value="&nbsp;&nbsp;Search&nbsp;&nbsp;" class="searchbtn"/>
       </form> 

【问题讨论】:

  • 请注意:success() 已弃用。请改用done()

标签: php mysql codeigniter


【解决方案1】:

避免使用大量类似的运算符。全文搜索是要走的路。默认情况下,完整的 txtsearch 不区分大小写。所以你不会有任何问题。

这里是如何将mysql全文与codeigniter集成

Using Match and Against in MySQL and CodeIgniter

【讨论】:

  • 请修改我的代码并添加您的逻辑代码。然后让我们测试一下
  • 这个很好还是很难将这样的查询与codeigniter集成?
  • 我试过这样:但我得到了类似“在 $res->result_array() 上调用非对象的错误。我的代码:$this-&gt;load-&gt;database(); $this-&gt;db-&gt;select("book_title","auth_firstname"); $this-&gt;db-&gt;where ("MATCH (book_title, auth_firstname) AGAINST ('{$keyword}')"); $this-&gt;db-&gt;or_where("book_title LIKE '%{$keyword}%'"); $this-&gt;db-&gt;or_where("auth_firstname LIKE '%{$keyword}%'"); $res = $this-&gt;db-&gt;get('bookdetails');$ret = array(); foreach ($res-&gt;result_array() as $row)
  • 您是在表格中添加全文搜索还是只运行查询?
【解决方案2】:

您是否有意在表格上使用区分大小写的排序规则?更改为不区分大小写的排序规则将解决您的问题。另一种解决方案是将您的字符串匹配为小写(或大写)。像这样的:

$this->db->or_like('LOWER(auth_lastname)', strtolower($keyword));

另外,如果你在 mysql 中使用 InnoDB 作为存储引擎,则无法进行全文搜索

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2011-01-18
    • 2013-04-17
    • 2015-09-02
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多