PHPackages                             dobron/search-query-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. dobron/search-query-parser

AbandonedArchivedLibrary

dobron/search-query-parser
==========================

A simple parser for advanced search query syntax in PHP.

2.0.0(5y ago)2131MITPHPPHP &gt;=7.4

Since Aug 9Pushed 5y ago1 watchersCompare

[ Source](https://github.com/richardDobron/search-query-parser)[ Packagist](https://packagist.org/packages/dobron/search-query-parser)[ RSS](/packages/dobron-search-query-parser/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

🔍 Search Query Parser
=====================

[](#-search-query-parser)

The Query String Parser library performs search query text parsing.

This library is perfect for integrating complex search (like Google search) into your application. Small part of base code (javascript) is come from

Example usage of Parser:
------------------------

[](#example-usage-of-parser)

```
require (__DIR__."/vendor/autoload.php");

$parser = new dobron\SearchQueryParser\Parser([
  'keywords' => 'site,title,inurl'
]);
$result = $parser->parse('site:en.wikipedia.org/ title:Slovakia "cities and towns" -education inurl:wiki/');

echo json_encode($result, JSON_PRETTY_PRINT);

var_dump($result->getQueries());
```

Output:
-------

[](#output)

```
{
    "text": [
        {
            "column": "text",
            "operator": "like",
            "negate": false,
            "value": "%cities and towns%"
        }
    ],
    "match": {
        "site": {
            "column": "site",
            "operator": "=",
            "negate": false,
            "value": "en.wikipedia.org\/"
        },
        "title": {
            "column": "title",
            "operator": "=",
            "negate": false,
            "value": "Slovakia"
        },
        "inurl": {
            "column": "inurl",
            "operator": "=",
            "negate": false,
            "value": "wiki\/"
        }
    },
    "excluded": {
        "text": [
            {
                "column": "text",
                "operator": "not like",
                "negate": true,
                "value": "%education%"
            }
        ]
    },
    "offsets": [
        {
            "keyword": "site",
            "value": "en.wikipedia.org\/",
            "offsetStart": 0,
            "offsetEnd": 22
        },
        {
            "keyword": "title",
            "value": "Slovakia",
            "offsetStart": 23,
            "offsetEnd": 37
        },
        {
            "text": "cities and towns",
            "offsetStart": 38,
            "offsetEnd": 54
        },
        {
            "keyword": "inurl",
            "value": "wiki\/",
            "offsetStart": 68,
            "offsetEnd": 79
        }
    ]
}
```

```
array(5) {
  [0]=>
  array(4) {
    ["column"]=>
    string(4) "site"
    ["operator"]=>
    string(1) "="
    ["negate"]=>
    bool(false)
    ["value"]=>
    string(17) "en.wikipedia.org/"
  }
  [1]=>
  array(4) {
    ["column"]=>
    string(5) "title"
    ["operator"]=>
    string(1) "="
    ["negate"]=>
    bool(false)
    ["value"]=>
    string(8) "Slovakia"
  }
  [2]=>
  array(4) {
    ["column"]=>
    string(5) "inurl"
    ["operator"]=>
    string(1) "="
    ["negate"]=>
    bool(false)
    ["value"]=>
    string(5) "wiki/"
  }
  [3]=>
  array(4) {
    ["column"]=>
    string(4) "text"
    ["operator"]=>
    string(8) "not like"
    ["negate"]=>
    bool(true)
    ["value"]=>
    string(11) "%education%"
  }
  [4]=>
  array(4) {
    ["column"]=>
    string(4) "text"
    ["operator"]=>
    string(4) "like"
    ["negate"]=>
    bool(false)
    ["value"]=>
    string(18) "%cities and towns%"
  }
}

```

### Options:

[](#options)

- `keywords`, that can be separated by commas (,). Accepts an array of strings.
- `ranges`, that can be separated by a hyphen (-). Accepts an array of strings.
- `offsets`, a boolean controls the behaviour of the returned query. If set to `true`, the query will contain the offsets object. If set to `false`, the query will not contain the offsets object. Defaults to `true`.

Example usage of Compiler:
--------------------------

[](#example-usage-of-compiler)

```
require(__DIR__."/vendor/autoload.php");

$parser = new dobron\SearchQueryParser\Compiler([
    // field, value
    ['site', 'en.wikipedia.org/'],

    // field, value
    ['title', 'Slovakia'],

    // value (array or string)
    [['cities and towns']],

    // or:
    // value, negate
    // ['cities and towns', false],

    // field, value, operator, negate
    [null, 'education', '=', true],

    // field, value, operator
    ['inurl', 'wiki/', '=']
]);

echo $parser->compile();
```

Output:
-------

[](#output-1)

```
site:en.wikipedia.org/ title:Slovakia "cities and towns" -education inurl:wiki/

```

### Options:

[](#options-1)

- `alwaysQuote`, a boolean controls the behaviour of the searched query. If set to `true`, the query will contain the quotes. If set to `false`, the query will not contain the quotes. Defaults to `false`.

### Queries:

[](#queries)

TypeExampleQuery for tags`cat`Query for multiple tags`cat dog`, `"Hello World"`Exclude results containing a certain word`cat -dog`Query for equality`author:John Snow`, `author:"John Snow"`Query for multiple equality`author:me,John Snow`Query for values in range`date:2000/01/01-2020/01/01`, `price:5-50`Query for values greater than another value`price:>10`, `price:>=10`Query for values less than another value`price:
