/** * Formatting file names * * @param string $filename The filename to format * @param boolean $lowercase Should we return the name un lowercase (FALSE by default) * @param string $delimiter The delimiter used for special chars substitution * @return string A filename valid on (almost) all systems */ public static function formatFilename($filename = '', $lowercase = false, $delimiter = '-') { if (empty($filename)) { return ''; } $_ext = self::getExtension($filename, true); if ($_ext) { $filename = str_replace($_ext, '', $filename); } $string = $filename; if ($lowercase) { $string = strtolower($string); } $string = str_replace(" ", $delimiter, $string); $string = preg_replace('#\\-+#', $delimiter, $string); $string = preg_replace('#([-]+)#', $delimiter, $string); $string = trim($string, $delimiter); $string = TextHelper::stripSpecialChars($string, $delimiter . '.'); if ($_ext) { $string .= $_ext; } return $string; }
public static function getSafeIdString($string) { return TextHelper::stripSpecialChars(TextHelper::slugify($string), '-_'); }
/** * @covers ../../../src/Library/Helper/Text::stripSpecialChars() */ public function testStripSpecialChars() { $this->checkNoArg('stripSpecialChars'); $str = 'Lorem é ipsum à amet §!* lorem'; $str_a = 'Loremeipsumaametlorem'; $str_b = 'Lorem e ipsum a amet lorem'; $this->assertEquals($str_a, \Library\Helper\Text::stripSpecialChars($str)); $this->assertEquals($str_b, \Library\Helper\Text::stripSpecialChars($str, ' ')); }