1 transliteration.inc transliteration_clean_filename($filename, $source_langcode = NULL)

Transliterates and sanitizes a file name.

The resulting file name has white space replaced with underscores, consists of only US-ASCII characters, and is converted to lowercase (if configured). If multiple files have been submitted as an array, the names will be processed recursively.

Parameters

$filename: A file name, or an array of file names.

$source_langcode: Optional ISO 639 language code that denotes the language of the input and is used to apply language-specific variations. If the source language is not known at the time of transliteration, it is recommended to set this argument to the site default language to produce consistent results. Otherwise the current display language will be used.

Return value

Sanitized file name, or array of sanitized file names.

See also

language_default()

File

core/includes/transliteration.inc, line 241
Transliteration processing functions.

Code

function transliteration_clean_filename($filename, $source_langcode = NULL) {
  if (is_array($filename)) {
    foreach ($filename as $key => $value) {
      $filename[$key] = transliteration_clean_filename($value, $source_langcode);
    }
    return $filename;
  }

  // Allow other modules to alter the filename prior to processing.
  backdrop_alter('transliteration_clean_filename_prepare', $filename, $source_langcode);
  $filename = transliteration_get($filename, '', $source_langcode);
  $filename = str_replace(' ', '_', $filename);
  $filename = preg_replace('/[^a-z0-9_.-]+/i', '', $filename);
  // Force lowercase to prevent issues on case-insensitive file systems.
  if (config_get('system.core', 'file_transliterate_lowercase')) {
    $filename = strtolower($filename);
  }
  return $filename;
}