有没有办法在过滤数据表开始时忽略空格


is there a way to ignore spaces at the beginning of filtering for datatables?

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

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

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

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

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

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

我不确定 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...