2011-02-07 26 views
5

Cách tốt nhất để chuyển đổi đầu vào sau vào SQL bằng cách sử dụng PHP là gì?Chuyển các truy vấn tùy chỉnh thành SQL

+a - includes a 
+(c\d) - includes c or d but not both 
+(c/d/e) - includes any of these 
-f - does not include f 

Tôi đã sử dụng preg_replace khác nhau hoặc phát nổ để lặp lại và thực hiện các câu lệnh nhưng không có bất kỳ điều gì để hoạt động ổn định.

tôi về cơ bản cần phải bật cái gì đó như

+a +(c/d/e) 

Into

SELECT * FROM the_table 
WHERE description LIKE "%a%" 
AND (description LIKE "%c%" OR description like "%d%" OR description LIKE "%$e%") 

Cảm ơn!

CẬP NHẬT:

Đây là nỗ lực của tôi. Tôi chắc chắn có một cách dễ dàng hơn ...

public function custom_query($query){ 

    $fields = " feed_items.description "; 

    $return_query = ""; 

    $query = str_replace("'", '', $query); 

    $pluses = explode('+',$query); 

    foreach($pluses as $plus){ 

      $plus = trim($plus); 

      if (substr($plus,0,1) == '('){ 
       $return_query .= 'AND ('; 

       $plus = str_replace(array('(',')'), '', $plus); 

       $ors = explode('/', $plus); 

       foreach($ors as $or){ 
        if ($or){ 
         $return_query .= $fields." LIKE '%".$or."%' OR"; 
        } 
       } 

       $return_query = rtrim($return_query, ' OR'); 
       $return_query .= ')'; 
      } else { 
       if ($plus){ 
        $return_query .= ' AND ' . $fields.' LIKE '.'"%'.$plus.'%"'; 
       } 
      } 

     } 

    $negatives = explode('-',$query); 

    foreach($negatives as $negative){ 

      $negative = trim($negative); 

      if (substr($negative,0,1) == '('){ 
       $return_query .= 'AND ('; 

       $negative = str_replace(array('(',')'), '', $negative); 

       $ors = explode('\\', $negative); 

       foreach($ors as $or){ 
        if ($or){ 
         $return_query .= $fields." NOT LIKE '%".$or."%' OR"; 
        } 
       } 

       $return_query = rtrim($return_query, ' OR'); 
       $return_query .= ')'; 
      } else { 
       if ($negative){ 
        $return_query .= ' AND ' . $fields.' NOT LIKE '.'"%'.$negative.'%"'; 
       } 
      } 

     } 

    $return_query = ' AND '.ltrim($return_query, ' AND '); 

    return $return_query; 

} 

Trả lời

1

Vì bạn không cần lồng biểu thức. Nó đơn giản và regex là thực sự chỉ lười biếng:

$sql = preg_replace_callback("' 
      ([+-])   # AND or NOT 
      (\w+    # capture a single word 
      | \(    # or something enclosed in literal braces 
       (\w+   # word 
        ([/\\\\])? # logical delimiter 
       [^)]*)  # remainder words and/or delimiters 
      \) 
      )'x", 
     "to_sql", $search_pattern); 

function to_sql($match) { 
    @list($uu, $logical, $word, $words, $or) = $match; 

    if ($logical == "+") { 
     $sql = " AND ("; 
    } 
    else { 
     $sql = " OR ("; 
    } 

    if ($words) { 
     $words = explode($or, $words); 
    } 
    else { 
     $words = array($word); 
     $or = "/"; 
    } 

    foreach ($words as $i=>$word) { 
     $words[$i] = "description LIKE '%$word%'"; 
    } 

    $or = ($or == "/") ? " OR " : " XOR "; 
    $sql .= implode($or, $words); 

    return "$sql)"; 
} 

nó sẽ trả lại tuyên bố bắt đầu bằng "AND", điều này có thể được điều chỉnh, nhưng nó dễ dàng hơn để lừa và chỉ cần thêm "TRUE" để biến nó thành một giá trị Mệnh đề WHERE.

1
$pattern = "+a +(c/d/e)"; 

// replace a, c, d, e with "description like "%something%" 
$pattern = preg_replace('#[^()+\\-\\\\/\s]#', 'description like "%$0%"', $pattern); 

// replace operators 
$replacements = array(
    '+' => ' and ', 
    '-' => ' and not ', 
    '\\' => ' xor ', 
    '/' => ' or ', 
); 
$pattern = str_replace(array_keys($replacements), $replacements, $pattern); 
$pattern = trim($pattern); 

// remove first "and" 
// * "and something and something_else" becomes "something and something_else" 
// * "and not something and something_else" becomes "not something and something else" 
$pattern = preg_replace('#^and #', '', $pattern); 

Patten nên bắt đầu với +, - hoặc không có bất kỳ nhà điều hành.

Không chắc về thay thế \ với xor - phụ thuộc vào cách bạn muốn xử lý (a\b\c), (a\b\c\d), vv

Các vấn đề liên quan