PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function parse_boolean_search($search, $conn) { $tokens = preg_split('/\s+(AND|OR|NOT)\s+/i', $search, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $sql_clauses = []; $is_not = false; $current_clause = ''; foreach ($tokens as $token) { if (strcasecmp($token, 'AND') === 0 || strcasecmp($token, 'OR') === 0) { if (!empty($current_clause)) { $sql_clauses[] = $current_clause; $current_clause = ''; } $sql_clauses[] = $token; $is_not = false; } elseif (strcasecmp($token, 'NOT') === 0) { $is_not = true; } else { $escaped_token = $conn->real_escape_string($token); if (preg_match('/^\/.*\/[a-z]*$/', $escaped_token)) { preg_match('/^\/(.*)\/([a-z]*)$/', $escaped_token, $regex_parts); $pattern = $regex_parts[1]; $clause = "(LOWER(description_full) REGEXP LOWER('$pattern') OR LOWER(description_short) REGEXP LOWER('$pattern'))"; } else { $clause = "(LOWER(description_full) LIKE LOWER('%$escaped_token%') OR LOWER(description_short) LIKE LOWER('%$escaped_token%'))"; } if ($is_not) { $current_clause .= "NOT $clause"; $is_not = false; } else { $current_clause .= $clause; } $current_clause .= ' AND '; } } if (!empty($current_clause)) { $sql_clauses[] = rtrim($current_clause, ' AND '); } return implode(' ', $sql_clauses); } $search_query = "gardena NOT trimmer"; $conn = "123"; $sql_where_clause = parse_boolean_search($search_query, $conn); echo $sql_where_clause; ?>
Show:  
Copy Clear