PHP library for parsing search strings

Version 2.0.3 (10/15/2015)
Price: Free / Donations

SearchstringParser is a library for PHP 5.3 or higher which will take a “typical” search-engine-like search string and split it into parts. It supports phrases, required, optional and excluded terms/phrases by using +, -, AND, NOT and OR.

If you use a search engine like Apache Solr which does the parsing itself, you may not need a library such as this. But in cases where you want switchable search backends but still have a consistent search syntax, or where the given search string will be used to find records in a SQL/NoSQL database, it provides simple and easy parsing. Equally, even when using a software such as Solr or Elasticsearch, it can be handy to control what is passed to the search engine, for instance to optimize the search syntax (example: by setting Solr’s mm parameter depending on the number of optional terms).


  • A term which is prefixed by + is regarded as required. Example: +word.
  • When there is an AND between two terms, both are regarded as required. Example: Hello AND World.
  • A term which is prefixed by - is regarded as excluded. Example: -word
  • A term which is preceded by NOT is regarded as excluded. Example: NOT word
  • When there is an OR between two terms, both are regarded as optional. Example: Hello OR World.
  • Phrases can be specified using double quotes. Double quotes inside a phrase can be escaped using a backslash. Example: "my \" phrase".
  • Everything said above regarding +, -, AND, OR, NOT applies to phrases as well.
  • Any term to which none of above rules applies, is by default regarded as an optional term . This can be changed by passing array('defaultOperator' => SearchstringParser:SYMBOL_AND) as argument 2 to SearchstringParser’s constructor to make such terms required.


  • Hello World ➔ Optional terms “Hello” and “World”, no required or excluded terms
  • Hello World -foobar ➜ Optional terms “Hello” and “World”, excluded term “foobar”, no required terms (Equivalent to: Hello World NOT foobar)
  • +"search string parser" "PHP 5.6" OR "PHP 5.3" NOT "PHP 4" NOT C# -C++ C ➔ Required phrase “search string parser”, optional phrases “PHP 5.6” and “PHP 5.3”, excluded phrases/terms “PHP 4”, “C#” and “C++” and skipped term “C” (which is shorter than the default minimum length of 2 characters)

Back to list of projects