dobron / search-query-parser
此软件包已被废弃,不再维护。未建议替代软件包。
PHP中高级搜索查询语法的简单解析器。
2.0.0
2020-08-12 07:22 UTC
Requires
- php: >=7.4
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.4@dev
This package is auto-updated.
Last update: 2024-04-12 14:56:48 UTC
README
查询字符串解析库执行搜索查询文本解析。
此库非常适合将复杂的搜索(如Google搜索)集成到您的应用程序中。部分基础代码(javascript)来自 https://github.com/nepsilon/search-query-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());
输出
{
"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%"
}
}
选项
keywords,可以用逗号(,)分隔。接受字符串数组。ranges,可以用连字符(-)分隔。接受字符串数组。offsets,一个布尔值控制返回查询的行为。如果设置为true,查询将包含偏移量对象。如果设置为false,查询将不包含偏移量对象。默认为true。
编译器使用示例
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();
输出
site:en.wikipedia.org/ title:Slovakia "cities and towns" -education inurl:wiki/
选项
alwaysQuote,一个布尔值控制搜索查询的行为。如果设置为true,查询将包含引号。如果设置为false,查询将不包含引号。默认为false。
查询
| 类型 | 示例 |
|---|---|
| 标签查询 | cat |
| 多个标签查询 | cat dog,"Hello World" |
| 排除包含特定单词的结果 | cat -dog |
| 等式查询 | author:John Snow,author:"John Snow" |
| 多个等式查询 | author:me,John Snow |
| 范围值查询 | date:2000/01/01-2020/01/01,price:5-50 |
| 大于另一个值的查询 | price:>10,price:>=10 |
| 小于另一个值的查询 | price:<100,price:<=100 |
| 标签和条件混合查询 | price:>10 price:<100 |
| 基于排除的筛选限定符 | price:>10 -language:php |
如何使用它?
使用以下命令安装
composer require dobron/search-query-parser
测试
php vendor/bin/phpunit --testdox tests