0

ユーザーが検索を行うときに、デフォルトですべての単語が必要になるようにする方法を見つけようとしています。

これは最初は簡単に思えました。単語を+分割し、各単語の先頭に記号を追加するだけです。ただし、他の演算子を実装しようとすると、複雑になります。

これは私がこれまでに持っているものです..

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);    

}

ご覧のとおり、現時点では引用符で囲まれた文字列を尊重していますが、分割しないことについても考え始め()、ネストされた二重引用符が含まれている場合、またはその逆の場合はどうでしょう.... .

だから私は、文字列を台無しにすることなく、ユーザーが明確に-.

4

1 に答える 1

0

パターン内で単語境界として preg_match() と \b を使用できるでしょうか?

検索用語は、次のようなもので分割できます

preg_match_all('/\b(.*)\b/', $matches);

ここで遅いので、私は間違った考えを持っているかもしれませんが、それはあなたに何かを続けるかもしれません

于 2013-06-08T17:10:40.820 に答える