Php 是否有一种方法可以在筛选数据表时忽略空格?
我使用jquery插件和php处理文件进行过滤。我已经修改了代码以允许多个关键字。但是如果我输入一个空格作为起始字符,我会得到一个JSON错误,是否可以忽略此错误而不必单击“确定”?或者有没有一种方法可以修改php以允许空格启动 谢谢 下面是一些代码:Php 是否有一种方法可以在筛选数据表时忽略空格?,php,jquery,datatables,Php,Jquery,Datatables,我使用jquery插件和php处理文件进行过滤。我已经修改了代码以允许多个关键字。但是如果我输入一个空格作为起始字符,我会得到一个JSON错误,是否可以忽略此错误而不必单击“确定”?或者有没有一种方法可以修改php以允许空格启动 谢谢 下面是一些代码: $sWhere = ""; if ( $_GET['sSearch'] != "") { $aWords = preg_split('/\s+/', $_GET['sSearch']);
$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 ";
}
}
$sWhere=”“;
如果($\u获取['s搜索]!=“”)
{
$aWords=preg_split('/\s+/',$_GET['sSearch']);
$sWhere=“WHERE(”;
对于($j=0;$j您的问题在于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...