我正在尝试找到一种方法来实现它,因此当用户进行搜索时,默认情况下需要所有单词。
一开始这似乎很容易,只需将单词分解并+
在每个单词的开头添加一个符号即可;但是,当您开始尝试实现其他运算符时,它会变得很复杂。
这是我到目前为止所拥有的..
function prepareKeywords($str) {
// Remove any + signs since we add them ourselves
// Also remove any operators we don't allow
// We don't allow some operators as they would have no use in the search since we don't order our results by relevance
$str = str_replace(array('+','~','<','>'), '', $str);
// Remove anything more than once space
$str = preg_replace('/\s{2,}/', ' ', $str);
// Now break up words into parts
$str = str_getcsv($str, ' ', '"');
// Now prepend a + sign to the front of each word
foreach ($ks as $key => $word) {
// First we check to make sure the start of the word doesn't already contain an operator before we add the + sign to the start
if (in_array($word{0}, array('-','<','>','~'))) {
} else {
$ks[$key] = '+' . $word;
}
}
// Now put word back to string
//$ks = implode(' ', $ks);
}
正如你所看到的,它目前只到了这么远,尊重引用的字符串,但后来我开始考虑不要分解()
,然后如果它包含嵌套的双引号,反之亦然......它开始变得非常毛茸茸.
所以我试图弄清楚是否有一种方法可以让我做我想做的事而不会弄乱字符串,并且只制作默认需要的所有单词,除非用户专门用-
.