2012-04-24 36 views
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 "; 
        } 
      } 

回答

1

你的問題是與preg_split()在單空間字符串操作:

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

將單個空間將返回兩數組空白字符串。更改第一行是:

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

這樣,你不要試圖用基本是空白字符串運行代碼。

+0

啊哈哈!非常感謝你,我從來沒有意識到這個偉大的信息! – arrowill12 2012-04-24 19:43:00

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... 
相關問題