phpbb\search\fulltext_mysql::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];
        // We limit the number of allowed keywords to minimize load on the database
        if ($this->config['max_num_search_keywords'] && sizeof($this->split_words) > $this->config['max_num_search_keywords']) {
            trigger_error($this->user->lang('MAX_NUM_SEARCH_KEYWORDS_REFINE', (int) $this->config['max_num_search_keywords'], sizeof($this->split_words)));
        }
        // to allow phrase search, we need to concatenate quoted words
        $tmp_split_words = array();
        $phrase = '';
        foreach ($this->split_words as $word) {
            if ($phrase) {
                $phrase .= ' ' . $word;
                if (strpos($word, '"') !== false && substr_count($word, '"') % 2 == 1) {
                    $tmp_split_words[] = $phrase;
                    $phrase = '';
                }
            } else {
                if (strpos($word, '"') !== false && substr_count($word, '"') % 2 == 1) {
                    $phrase = $word;
                } else {
                    $tmp_split_words[] = $word;
                }
            }
        }
        if ($phrase) {
            $tmp_split_words[] = $phrase;
        }
        $this->split_words = $tmp_split_words;
        unset($tmp_split_words);
        unset($phrase);
        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_mysql_min_word_len'] || $clean_len > $this->config['fulltext_mysql_max_word_len']) {
                $this->common_words[] = $word;
                unset($this->split_words[$i]);
            }
        }
        if ($terms == 'any') {
            $this->search_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 . ' ';
            }
        } else {
            $this->search_query = '';
            foreach ($this->split_words as $word) {
                if (strpos($word, '+') === 0 || strpos($word, '-') === 0) {
                    $this->search_query .= $word . ' ';
                } else {
                    if (strpos($word, '|') === 0) {
                        $this->search_query .= substr($word, 1) . ' ';
                    } else {
                        $this->search_query .= '+' . $word . ' ';
                    }
                }
            }
        }
        $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;
    }