/** * Converts a given string to a string that can be used as a URL segment. * The result is not url-encoded. * * @param string $string * @param string $spaceCharacter * @return string */ public function convertToSafeString($string, $spaceCharacter = '-') { $processedTitle = $this->csConvertor->conv_case('utf-8', $string, 'toLower'); $processedTitle = strip_tags($processedTitle); $processedTitle = preg_replace('/[ \\-+_]+/', $spaceCharacter, $processedTitle); $processedTitle = $this->csConvertor->specCharsToASCII('utf-8', $processedTitle); $processedTitle = preg_replace('/[^\\p{L}0-9' . preg_quote($spaceCharacter) . ']/u', '', $processedTitle); $processedTitle = preg_replace('/' . preg_quote($spaceCharacter) . '{2,}/', $spaceCharacter, $processedTitle); $processedTitle = trim($processedTitle, $spaceCharacter); // TODO Post-processing hook here $processedTitle = strtolower($processedTitle); return $processedTitle; }
/** * Returns a string where any character not matching [.a-zA-Z0-9_-] is substituted by '_' * Trailing dots are removed * * @param string $fileName Input string, typically the body of a filename * @param string $charset Charset of the a filename (defaults to current charset; depending on context) * @return string Output string with any characters not matching [.a-zA-Z0-9_-] is substituted by '_' and trailing dots removed * @todo Deprecate, but still in use by the core * @deprecated but still in use in the Core. Don't use in your extensions! */ public function cleanFileName($fileName, $charset = '') { // Handle UTF-8 characters if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) { // allow ".", "-", 0-9, a-z, A-Z and everything beyond U+C0 (latin capital letter a with grave) $cleanFileName = preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . ']/u', '_', trim($fileName)); } else { // Get conversion object or initialize if needed if (!is_object($this->csConvObj)) { $this->csConvObj = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Charset\CharsetConverter::class); } // Define character set if (!$charset) { if (TYPO3_MODE == 'FE') { $charset = $GLOBALS['TSFE']->renderCharset; } else { // Backend $charset = 'utf-8'; } } // If a charset was found, convert filename if ($charset) { $fileName = $this->csConvObj->specCharsToASCII($charset, $fileName); } // Replace unwanted characters by underscores $cleanFileName = preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . '\\xC0-\\xFF]/', '_', trim($fileName)); } // Strip trailing dots and return return rtrim($cleanFileName, '.'); }