function GetBestCategory($search_data) { if (Cache_Memory::IsCached(CACHE_CATEGORIES)) { $categories = Cache_Memory::Get(CACHE_CATEGORIES); } else { $DB = GetDB(); $categories = $DB->FetchAll('SELECT * FROM `tbx_category`'); Cache_Memory::Cache(CACHE_CATEGORIES, $categories); } $best_score = 0; $best_category_id = null; foreach ($categories as $category) { if (!String::IsEmpty($category['auto_category_term']) && preg_match('~(' . str_replace(',', '|', preg_quote($category['auto_category_term'])) . ')~i', $search_data, $matches)) { if (count($matches[1]) > $best_score) { $best_score = count($matches[1]); $best_category_id = $category['category_id']; } } } return $best_category_id; }