PHP库用于解析类似Google的搜索运算符?

我想创建一个 PHP搜索功能,但使用类似Google的运算符.例如:
these words "this phrase" location:"Los Angeles" operator:something

像location这样的运算符很重要:支持带有空格的值(因此这个例子中的引号),所以我不能只是拆分或使用标准令牌.我想有人在某个时候创建​​了一个用于执行此操作的库,但我找不到任何库.

或者如果它不需要库,那​​么这样做的好方法就是好的.

它只是解析我需要的搜索查询;即从上面的查询中得到一个由以下组成的数组是可以的:

[0] => these
[1] => words
[2] => "this phrase"
[3] => location:"Los Angeles"
[4] => operator:something

从那以后我可以为数据库构建搜索功能.

您可以从 str_getcsv()开始并使用空格作为分隔符,但您可能需要对位置进行预处理.运算符处理该特定情况下的引号.
<?PHP
$str = 'these words "this phrase" location:"Los Angeles" operator:something';

// preprocess the cases where you have colon separated deFinitions with quotes
// i.e. location:"los angeles"
$str = preg_replace('/(\w+)\:"(\w+)/','"${1}:${2}',$str);

$str = str_getcsv($str,' ');

var_dump($str);
?>

产量

array(5) {
  [0]=>
  string(5) "these"
  [1]=>
  string(5) "words"
  [2]=>
  string(11) "this phrase"
  [3]=>
  string(20) "location:Los Angeles"
  [4]=>
  string(18) "operator:something"
}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...