Parses a simplified SQL-like query string into a syntax tree which can be used to build search queries

v1.3.1 2021-05-26 09:55 UTC


Build Status

A basic search query parsing library which can be used to transform free-text searches into SQL (or other) queries. The analyzed search query can be used to build complex (SQL) queries, searching for a term in multiple fields. Take a look at the Zend_Db end to end test an example or have a look into examples/ for working examples.


Search terms can be AND/OR combined, negated, grouped with parentheses and modified to fuzzy exact search with the following syntax:

  • foo
  • foo* AND bar
  • *foo* OR *bar*
  • *foo* OR !*bar*
  • *foo* AND bar
  • foo OR (bar AND baz)


Fuzzy search: *<term>*
Negation: !<term> !(<query>) !*<fuzzy-term>



require_once __DIR__ . '/../vendor/autoload.php';

$input = 'doe AND 1212 AND !foo OR (!(am*ya AND 12) blah) OR baz'; // complex query
$input = '*john* !502* AND foobar'; // LIKE %john% AND NOT LIKE 502% AND = foobar

// tokenizes query string
$lexer = new \SearchQueryParser\Lexer();

// contains array of extracted tokens
$tokens = $lexer->lex($input);

// parses tokens into an abstract query
$parser = new \SearchQueryParser\Parser();
$query = $parser->parse($tokens);

// use the Zend_Db query builder to transform the Query into conditions
$db = new Zend_Db_Adapter_Pdo_Sqlite(array(
    'dbname' => ':memory:'

// dummy query
$select = $db

// build conditions for every passed field
$queryBuilder = new \SearchQueryParser\QueryBuilder\ZendDbSelect([

$queryBuilder->processQuery($select, $query);

Using the facade

There's a facade class which can be called if you want to use the standard lexer/parser.


require_once __DIR__ . '/../vendor/autoload.php';

$input = 'foo !bar *doe*';

// returns an abstract query


There are a couple of simple end-to-end tests checking the output for a defined input. Just run vendor/bin/phpunit from the project root.