1

我正在使用 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 ";
                    }
            }
4

2 回答 2

1

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

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

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

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

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

于 2012-04-24T19:38:02.860 回答
1

我不确定 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...
于 2012-04-24T19:42:34.220 回答