public function setQuery($query) { $this->original_query = $query; $this->query = array(); $stopwords = string_get_stopwords(cmsCore::getLanguageName()); $words = explode(' ', $query); foreach ($words as $word) { $word = strip_tags(mb_strtolower(trim(urldecode($word)))); if (mb_strlen($word) < 3 || is_numeric($word)) { continue; } if ($stopwords && in_array($word, $stopwords)) { continue; } if (mb_strlen($word) == 3) { $this->query[] = $this->db->escape($word); continue; } if (mb_strlen($word) >= 12) { $word = mb_substr($word, 0, mb_strlen($word) - 4); } else { if (mb_strlen($word) >= 10) { $word = mb_substr($word, 0, mb_strlen($word) - 3); } else { if (mb_strlen($word) >= 6) { $word = mb_substr($word, 0, mb_strlen($word) - 2); } else { $word = mb_substr($word, 0, mb_strlen($word) - 1); } } } $this->query[] = $this->db->escape($word) . '*'; } if (empty($this->query)) { return false; } return true; }
/** * Возвращает строку с перечислением самых часто используемых * слов из исходного текста * * @param string $text * @param int $min_length Минимальная длина каждого слова * @param int $limit Количество слов в результирующей строке * @return string */ function string_get_meta_keywords($text, $min_length = 5, $limit = 10) { $stat = array(); $text = str_replace(array("\n", '<br>', '<br/>'), ' ', $text); $text = strip_tags($text); $text = mb_strtolower($text); $stopwords = string_get_stopwords(cmsConfig::get('language')); $words = explode(' ', $text); foreach ($words as $word) { $word = trim($word); $word = str_replace(array('(', ')', '+', '-', '.', '!', ':', '{', '}', '|', '"', ',', "'"), '', $word); $word = preg_replace("/\\.,\\(\\)\\{\\}/i", '', $word); if ($stopwords && in_array($word, $stopwords)) { continue; } if (mb_strlen($word) >= $min_length) { $stat[$word] = isset($stat[$word]) ? $stat[$word] + 1 : 1; } } asort($stat); $stat = array_reverse($stat, true); $stat = array_slice($stat, 0, $limit, true); return implode(', ', array_keys($stat)); }
public function filterRelated($field, $value, $lang = false) { if (mb_strlen($value) <= 3) { return $this->filterLike($field, $this->db->escape($value) . '%'); } $query = array(); $words = explode(' ', str_replace(array('"', '\'', '+', '*', '-', '%', ')', '(', '.', ',', '!', '?'), ' ', $value)); $stopwords = string_get_stopwords($lang ? $lang : cmsConfig::get('language')); foreach ($words as $word) { $word = mb_strtolower(trim($word)); if (mb_strlen($word) < 3 || is_numeric($word)) { continue; } if ($stopwords && in_array($word, $stopwords)) { continue; } if (mb_strlen($word) == 3) { $query[] = $this->db->escape($word); continue; } if (mb_strlen($word) >= 12) { $word = mb_substr($word, 0, mb_strlen($word) - 3) . '*'; } else { if (mb_strlen($word) >= 10) { $word = mb_substr($word, 0, mb_strlen($word) - 2) . '*'; } else { if (mb_strlen($word) >= 6) { $word = mb_substr($word, 0, mb_strlen($word) - 1) . '*'; } } } $query[] = $this->db->escape($word); } if (empty($query)) { $ft_query = '\\"' . $this->db->escape($value) . '\\"'; } else { usort($query, function ($a, $b) { return mb_strlen($b) - mb_strlen($a); }); $query = array_slice($query, 0, 5); $ft_query = '>\\"' . $this->db->escape($value) . '\\" <'; $ft_query .= implode(' ', $query); } if (strpos($field, '.') === false) { $field = 'i.' . $field; } $search_param = "MATCH({$field}) AGAINST ('{$ft_query}' IN BOOLEAN MODE)"; $this->select($search_param, 'fsort'); $this->order_by = "fsort desc"; return $this->filter($search_param); }