客户对我正在做的员工管理项目提出了这个新要求,以允许他们的用户进行自定义 bool 搜索。
基本上允许他们使用:AND、OR、NOT、括号和引号。
实现它的最佳方法是什么?我检查了 mysql,它们使用不同的语法。
这是测试场景:
要求:寻找来自新加坡的具有精英或老兵等级的魔法师或野蛮人
bool 字符串:("Magician"OR "Barbarian") AND (Elite OR "Veteran Level") and Singaporean
这是我将考虑修改的代码之一,我从 Pure PHP based boolean search for strings 获得的
function booleanSearch($content, $search) {
$criteria = str_getcsv($search, ' ');
while ($criteria) {
$not = false;
$q = array_shift($criteria);
if (substr($q, 0, 2) === '-"') {
$not = true;
while (substr($q, -1) != '"') {
$q .= " " . array_shift($criteria);
}
$q = substr($q, 2, -1);
}
else if (substr($q, 0, 1) === '-' && strpos($q, ' ') === false) {
$not = true;
$q = substr($q, 1);
}
$found = strpos($content, $q) !== false;
if ($found === $not) {
trả về sai;
}
}
trả về đúng sự thật;
}
Tôi là một lập trình viên xuất sắc, rất giỏi!