【问题标题】:is there a way to ignore spaces at the beginning of filtering for datatables?有没有办法在过滤数据表时忽略空格?
【发布时间】:2012-04-24 19:33:29
【问题描述】:

我正在使用 jquery 插件Datables 并且我正在使用 php 处理文件进行过滤。我已经修改了代码以允许使用多个关键字。但是如果我输入一个空格作为起始字符,我会收到 JSON 错误,是否可以忽略此错误而无需单击确定?或者有没有办法修改php以允许空格开始。

谢谢

这里有一些代码:

 $sWhere = "";
    if ( $_GET['sSearch'] != "")
    {
            $aWords = preg_split('/\s+/', $_GET['sSearch']);
            $sWhere = "WHERE (";

            for ( $j=0 ; $j<count($aWords) ; $j++ )
            {
                    if ( $aWords[$j] != "" )
                    {
                            if(substr($aWords[$j], 0, 1) == "!"){
                                    $notString = substr($aWords[$j], 1);
                                    $sWhere .= "(";
                                    for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
                                            $sWhere .= $aColumns[$i]." NOT LIKE '%".mysql_real_escape_string( $notString )."%' AND ";
                                    }
                                    $sWhere = substr_replace( $sWhere, "", -4 );
                            }
                            else{
                                    $sWhere .= "(";
                                    for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
                                            $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $aWords[$j] )."%' OR ";
                                    }
                                    $sWhere = substr_replace( $sWhere, "", -3 );
                            }
                            $sWhere .= ") AND ";
                    }
            }

【问题讨论】:

    标签: php jquery datatables


    【解决方案1】:

    您的问题在于 preg_split() 对单个空格字符串进行操作:

    $e = preg_split('/\s+/', " ");
    print_r($e);
    

    拆分一个空格将返回一个由两个空白字符串组成的数组。将第一行更改为:

    $term = trim($_GET['sSearch']);
    if ( $term != "")
    {
            $aWords = preg_split('/\s+/', $term);
    

    这样,您就不会尝试使用基本上是空白的字符串来运行代码。

    【讨论】:

      【解决方案2】:

      我不确定 json 错误发生在哪里,因为您只显示了 php,但是 php 和 jQuery 都提供了从字符串的开头和结尾修剪空格的函数。

      在您的 javascript 中,在其余处理之前,您可以这样做:

      my_string = $.trim(original_string);
      

      在php中你可以这样做:

      $aWords = preg_split('/\s+/', trim($_GET['sSearch']));
      // or use trim on the individual words of the result...
      

      【讨论】:

        猜你喜欢
        • 2022-01-20
        • 1970-01-01
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多