phpbb\search\fulltext_postgres::split_keywords PHP Method

split_keywords() public method

Splits keywords entered by a user into an array of words stored in $this->split_words Stores the tidied search query in $this->search_query
public split_keywords ( &$keywords, string $terms ) : boolean
$terms string is either 'all' or 'any'
return boolean false if no valid keywords were found and otherwise true
    public function split_keywords(&$keywords, $terms)
    {
        if ($terms == 'all') {
            $match = array('#\\sand\\s#iu', '#\\sor\\s#iu', '#\\snot\\s#iu', '#(^|\\s)\\+#', '#(^|\\s)-#', '#(^|\\s)\\|#');
            $replace = array(' +', ' |', ' -', ' +', ' -', ' |');
            $keywords = preg_replace($match, $replace, $keywords);
        }
        // Filter out as above
        $split_keywords = preg_replace("#[\"\n\r\t]+#", ' ', trim(htmlspecialchars_decode($keywords)));
        // Split words
        $split_keywords = preg_replace('#([^\\p{L}\\p{N}\'*"()])#u', '$1$1', str_replace('\'\'', '\' \'', trim($split_keywords)));
        $matches = array();
        preg_match_all('#(?:[^\\p{L}\\p{N}*"()]|^)([+\\-|]?(?:[\\p{L}\\p{N}*"()]+\'?)*[\\p{L}\\p{N}*"()])(?:[^\\p{L}\\p{N}*"()]|$)#u', $split_keywords, $matches);
        $this->split_words = $matches[1];
        foreach ($this->split_words as $i => $word) {
            $clean_word = preg_replace('#^[+\\-|"]#', '', $word);
            // check word length
            $clean_len = utf8_strlen(str_replace('*', '', $clean_word));
            if ($clean_len < $this->config['fulltext_postgres_min_word_len'] || $clean_len > $this->config['fulltext_postgres_max_word_len']) {
                $this->common_words[] = $word;
                unset($this->split_words[$i]);
            }
        }
        if ($terms == 'any') {
            $this->search_query = '';
            $this->tsearch_query = '';
            foreach ($this->split_words as $word) {
                if (strpos($word, '+') === 0 || strpos($word, '-') === 0 || strpos($word, '|') === 0) {
                    $word = substr($word, 1);
                }
                $this->search_query .= $word . ' ';
                $this->tsearch_query .= '|' . $word . ' ';
            }
        } else {
            $this->search_query = '';
            $this->tsearch_query = '';
            foreach ($this->split_words as $word) {
                if (strpos($word, '+') === 0) {
                    $this->search_query .= $word . ' ';
                    $this->tsearch_query .= '&' . substr($word, 1) . ' ';
                } else {
                    if (strpos($word, '-') === 0) {
                        $this->search_query .= $word . ' ';
                        $this->tsearch_query .= '&!' . substr($word, 1) . ' ';
                    } else {
                        if (strpos($word, '|') === 0) {
                            $this->search_query .= $word . ' ';
                            $this->tsearch_query .= '|' . substr($word, 1) . ' ';
                        } else {
                            $this->search_query .= '+' . $word . ' ';
                            $this->tsearch_query .= '&' . $word . ' ';
                        }
                    }
                }
            }
        }
        $this->tsearch_query = substr($this->tsearch_query, 1);
        $this->search_query = utf8_htmlspecialchars($this->search_query);
        if ($this->search_query) {
            $this->split_words = array_values($this->split_words);
            sort($this->split_words);
            return true;
        }
        return false;
    }