Ok, that will be a great answer.
I think you need a parser generator. The piece of software that generates code for analyzing text in accordance with this grammar. These parsers often have 2 main components: lexer and parser. The lexer identifies the TOKENS (words), the parser checks the correct order of the marker according to your grammar.
In the lexer, you must declare the following tokens
TOKENS ::= (AND, OR, NOT, WORD, WORDSTAR, LPAREN, RPAREN, QUOTE) WORD ::= '/w+/' WORDSTAR ::= '/w+\*/'
Grammar should be defined as follows:
QUERY ::= word QUERY ::= wordstar QUERY ::= lparen QUERY rparen QUERY ::= QUERY and QUERY QUERY ::= QUERY or QUERY QUERY ::= QUERY and not QUERY QUERY ::= quote MQUERY quote MQUERY ::= word MQUERY MQUERY ::= word
This grammar defines a language with all the functions you need. Depending on the software you use, you can define functions for processing each rule. That way you can convert your text query into a sql where clause.
I am not in php, but I searched the Internet for a parser generator and PHP_ParserGenerator .
Keep in mind that as your database grows, these queries can be a problem for structured storage.
You can try the full-text search engine, which allows you to perform this and many other functions related to text search. Here's how IndexTank works
First you add (or "index" to the search dialect) all your db entries (or documents) in IndexTank.
$api = new ApiClient(...); $index = $api->get_index('my_index'); foreach ($dbRows as $row) { $index->add_document($row->id, array('text' => $row->text)); }
After that you can search in the index with all the operators you want
$index = $api->get_index('my_index'); $search_result = $index->search('Apples AND Oranges'); $search_result = $index->search('Apples OR Oranges'); $search_result = $index->search('Apples AND NOT Oranges'); $search_result = $index->search('"apples oranges"'); $search_result = $index->search('Apples AND ( Oranges OR Pears )'); $search_result = $index->search('Appl*');
I hope I answered your question.