/** * Convert a string to a URL safe string * * @param string $str * @param array $replace * @param string $separator * @return string */ protected function urlSafe($str, $replace = array(), $separator = '-') { if (!empty($replace)) { $str = str_replace((array) $replace, ' ', $str); } // Transliterate non-ASCII characters // $str = UTF8::transliterate_to_ascii($str); // Remove all characters that are not the separator, a-z, 0-9, or whitespace $str = preg_replace('![^' . preg_quote($separator) . 'a-z0-9\\s]+!', '', UTF8::strtolower($str)); // Replace all separator characters and whitespace by a single separator $str = preg_replace('![' . preg_quote($separator) . '\\s]+!u', $separator, $str); // Trim separators from the beginning and end return trim($str, $separator); }
/** * @covers Patchwork\Utf8::strtolower * @covers Patchwork\Utf8::strtoupper */ function testStrCase() { $this->assertSame('déjà σσς', u::strtolower('DÉJÀ Σσς')); $this->assertSame('DÉJÀ ΣΣΣ', u::strtoupper('Déjà Σσς')); }
/** * Makes a string lowercase. * @param string $string The string being lowercased. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default. * @return string Returns the string with all alphabetic characters converted to lowercase. * This function is aimed at replacing the functions strtolower() and mb_strtolower() for human-language strings. * @link http://php.net/manual/en/function.strtolower * @link http://php.net/manual/en/function.mb-strtolower */ function api_strtolower($string, $encoding = null) { return Utf8::strtolower($string); }
public function lower() { $this->string = u::strtolower($this->string); return $this; }
/** * Make a string lowercase * * @param string $str * * @return string * * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0. * Use Patchwork\Utf8::strtolower() instead. */ function utf8_strtolower($str) { @trigger_error('Using utf8_strtolower() has been deprecated and will no longer work in Contao 5.0. Use Patchwork\\Utf8::strtolower() instead.', E_USER_DEPRECATED); return Utf8::strtolower($str); }
/** * Search the index and return the result object * * @param string $strKeywords The keyword string * @param boolean $blnOrSearch If true, the result can contain any keyword * @param array $arrPid An optional array of page IDs to limit the result to * @param integer $intRows An optional maximum number of result rows * @param integer $intOffset An optional result offset * @param boolean $blnFuzzy If true, the search will be fuzzy * * @return Database\Result The database result object * * @throws \Exception If the cleaned keyword string is empty */ public static function searchFor($strKeywords, $blnOrSearch = false, $arrPid = array(), $intRows = 0, $intOffset = 0, $blnFuzzy = false) { // Clean the keywords $strKeywords = Utf8::strtolower($strKeywords); $strKeywords = \StringUtil::decodeEntities($strKeywords); $strKeywords = preg_replace(array('/\\. /', '/\\.$/', '/: /', '/:$/', '/, /', '/,$/', '/[^\\w\' *+".:,-]/u'), ' ', $strKeywords); // Check keyword string if (!strlen($strKeywords)) { throw new \Exception('Empty keyword string'); } // Split keywords $arrChunks = array(); preg_match_all('/"[^"]+"|[\\+\\-]?[^ ]+\\*?/', $strKeywords, $arrChunks); $arrPhrases = array(); $arrKeywords = array(); $arrWildcards = array(); $arrIncluded = array(); $arrExcluded = array(); foreach ($arrChunks[0] as $strKeyword) { if (substr($strKeyword, -1) == '*' && strlen($strKeyword) > 1) { $arrWildcards[] = str_replace('*', '%', $strKeyword); continue; } switch (substr($strKeyword, 0, 1)) { // Phrases case '"': if (($strKeyword = trim(substr($strKeyword, 1, -1))) != false) { $arrPhrases[] = '[[:<:]]' . str_replace(array(' ', '*'), array('[^[:alnum:]]+', ''), $strKeyword) . '[[:>:]]'; } break; // Included keywords // Included keywords case '+': if (($strKeyword = trim(substr($strKeyword, 1))) != false) { $arrIncluded[] = $strKeyword; } break; // Excluded keywords // Excluded keywords case '-': if (($strKeyword = trim(substr($strKeyword, 1))) != false) { $arrExcluded[] = $strKeyword; } break; // Wildcards // Wildcards case '*': if (strlen($strKeyword) > 1) { $arrWildcards[] = str_replace('*', '%', $strKeyword); } break; // Normal keywords // Normal keywords default: $arrKeywords[] = $strKeyword; break; } } // Fuzzy search if ($blnFuzzy) { foreach ($arrKeywords as $strKeyword) { $arrWildcards[] = '%' . $strKeyword . '%'; } $arrKeywords = array(); } // Count keywords $intPhrases = count($arrPhrases); $intWildcards = count($arrWildcards); $intIncluded = count($arrIncluded); $intExcluded = count($arrExcluded); $intKeywords = 0; $arrValues = array(); // Remember found words so we can highlight them later $strQuery = "SELECT tl_search_index.pid AS sid, GROUP_CONCAT(word) AS matches"; // Get the number of wildcard matches if (!$blnOrSearch && $intWildcards) { $strQuery .= ", (SELECT COUNT(*) FROM tl_search_index WHERE (" . implode(' OR ', array_fill(0, $intWildcards, 'word LIKE ?')) . ") AND pid=sid) AS wildcards"; $arrValues = array_merge($arrValues, $arrWildcards); } // Count the number of matches $strQuery .= ", COUNT(*) AS count"; // Get the relevance $strQuery .= ", SUM(relevance) AS relevance"; // Get meta information from tl_search $strQuery .= ", tl_search.*"; // see #4506 // Prepare keywords array $arrAllKeywords = array(); // Get keywords if (!empty($arrKeywords)) { $arrAllKeywords[] = implode(' OR ', array_fill(0, count($arrKeywords), 'word=?')); $arrValues = array_merge($arrValues, $arrKeywords); $intKeywords += count($arrKeywords); } // Get included keywords if ($intIncluded) { $arrAllKeywords[] = implode(' OR ', array_fill(0, $intIncluded, 'word=?')); $arrValues = array_merge($arrValues, $arrIncluded); $intKeywords += $intIncluded; } // Get keywords from phrases if ($intPhrases) { foreach ($arrPhrases as $strPhrase) { $arrWords = explode('[^[:alnum:]]+', Utf8::substr($strPhrase, 7, -7)); $arrAllKeywords[] = implode(' OR ', array_fill(0, count($arrWords), 'word=?')); $arrValues = array_merge($arrValues, $arrWords); $intKeywords += count($arrWords); } } // Get wildcards if ($intWildcards) { $arrAllKeywords[] = implode(' OR ', array_fill(0, $intWildcards, 'word LIKE ?')); $arrValues = array_merge($arrValues, $arrWildcards); } $strQuery .= " FROM tl_search_index LEFT JOIN tl_search ON(tl_search_index.pid=tl_search.id) WHERE (" . implode(' OR ', $arrAllKeywords) . ")"; // Get phrases if ($intPhrases) { $strQuery .= " AND (" . implode($blnOrSearch ? ' OR ' : ' AND ', array_fill(0, $intPhrases, 'tl_search_index.pid IN(SELECT id FROM tl_search WHERE text REGEXP ?)')) . ")"; $arrValues = array_merge($arrValues, $arrPhrases); } // Include keywords if ($intIncluded) { $strQuery .= " AND tl_search_index.pid IN(SELECT pid FROM tl_search_index WHERE " . implode(' OR ', array_fill(0, $intIncluded, 'word=?')) . ")"; $arrValues = array_merge($arrValues, $arrIncluded); } // Exclude keywords if ($intExcluded) { $strQuery .= " AND tl_search_index.pid NOT IN(SELECT pid FROM tl_search_index WHERE " . implode(' OR ', array_fill(0, $intExcluded, 'word=?')) . ")"; $arrValues = array_merge($arrValues, $arrExcluded); } // Limit results to a particular set of pages if (!empty($arrPid) && is_array($arrPid)) { $strQuery .= " AND tl_search_index.pid IN(SELECT id FROM tl_search WHERE pid IN(" . implode(',', array_map('intval', $arrPid)) . "))"; } $strQuery .= " GROUP BY tl_search_index.pid"; // Make sure to find all words if (!$blnOrSearch) { // Number of keywords without wildcards $strQuery .= " HAVING count >= " . $intKeywords; // Dynamically add the number of wildcard matches if ($intWildcards) { $strQuery .= " + IF(wildcards>" . $intWildcards . ", wildcards, " . $intWildcards . ")"; } } // Sort by relevance $strQuery .= " ORDER BY relevance DESC"; // Return result $objResultStmt = \Database::getInstance()->prepare($strQuery); if ($intRows > 0) { $objResultStmt->limit($intRows, $intOffset); } return $objResultStmt->execute($arrValues); }