寻找最佳方法来解析复杂的搜索字符串并将其减少为在术语之间有 1 个 bool 运算符。运算符为 OR、AND、NOR。
例如: 1. OR AND NOR NOR AND AND Fred OR NOR AND Wilma NOR AND Barney OR “The and AND Flintstones” OR AND NOR NOR AND AND
OR AND NOR nor AND AND Fred OR NOR AND Wilma NOR and AND Barney OR “The and AND Flintstones” OR and NOR NOR AND AND(2014 年 5 月添加)
结果:
Fred OR Wilma NOR Barney OR "The and AND Flintstones"
注意:我正在寻找 PHP 代码实现。
最佳答案
最好的办法是逐字读取输入并在状态机中处理它。
沿线的东西:
define("STATE_DEFAULT", 0); // we're in regular text
define("STATE_OPERATOR", 1); // we found operator (AND|OR|NOR)
define("STATE_QUOTE",2); // we're inside quoted text
$input = 'OR AND NOR NOR AND AND Fred OR NOR AND Wilma NOR AND AND Barney OR "The and AND Flintstones" OR AND NOR NOR AND AND';
// check if a word is an operator... used in multiple places
function _is_op($word) { return preg_match("/^(AND|OR|NOR)$/i", $word); }
$words = explode(" ", $input);
$words_count = count($words);
$state = STATE_DEFAULT;
for($i=0; $i<$words_count; ++$i)
{
$word = $words[$i];
switch($state)
{
case STATE_QUOTE:
if(substr($word,-1)=='"') $state = STATE_DEFAULT;
break;
case STATE_OPERATOR:
if(_is_op($word))
{
unset($words[$i]);
break;
}
case STATE_DEFAULT:
default:
$state = STATE_DEFAULT;
if($word[0] == '"')
$state = STATE_QUOTE;
elseif(_is_op($word))
$state = STATE_OPERATOR;
break;
}
}
// if we removed some words, count()-1 is no longer the last element
$words = array_values($words);
// strip operators from start and end
if(_is_op($words[0])) array_shift($words);
if(_is_op($words[count($words)-1])) array_pop($words);
$output = implode(" ", $words);
虽然可以使用正则表达式来做到这一点,但它会很复杂而且很难管理。
关于php - bool 搜索运算符优化,尊重引用的术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22049050/