我有一个项目,需要在纯PHP中进行一些基本的布尔搜索。这意味着我有纯字符串,我想在它们上提供一些简单的布尔搜索。不涉及数据库或其他索引引擎,因此请不要参考MySQL布尔搜索或lucene。
最后,类似以下代码的内容应该打印contains
和not found
。
$search = 'foo -bar "must have" -"must not have"';
$contentFound = 'This is some foo text you must have.';
$contentNotFound = 'This is some bar text you must have.';
if ($this->booleanSearch($contentFound, $search)) {
echo 'contains';
} else {
echo 'not found';
}
if ($this->booleanSearch($contentNotFound, $search)) {
echo 'contains';
} else {
echo 'not found';
}
对于一个简单的实现,您只需拆分标准(考虑引号),然后迭代每个标准,看看它是否匹配:
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) {
return false;
}
}
return true;
}