You are here

function transliteration_clean_filename in Transliteration 7.3

Same name and namespace in other branches
  1. 5.2 transliteration.inc \transliteration_clean_filename()
  2. 5 transliteration.inc \transliteration_clean_filename()
  3. 6.3 transliteration.module \transliteration_clean_filename()
  4. 6 transliteration.inc \transliteration_clean_filename()
  5. 6.2 transliteration.inc \transliteration_clean_filename()

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

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

string $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

mixed Sanitized file name, or array of sanitized file names.

See also

language_default()

3 calls to transliteration_clean_filename()
transliteration_init in ./transliteration.module
Implements hook_init().
transliteration_retroactive in ./transliteration.admin.inc
Form builder function; generates retroactive transliteration confirm form.
transliteration_retroactive_submit in ./transliteration.admin.inc
Form submit function; retroactively transliterates existing file names.

File

./transliteration.module, line 139
Converts non-latin text to US-ASCII and sanitizes file names.

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.
  drupal_alter('transliteration_clean_filename_prepare', $filename, $source_langcode);
  $filename = transliteration_get($filename, '', $source_langcode);

  // Replace whitespace.
  $filename = str_replace(' ', '_', $filename);

  // Enable underscore options.
  if (variable_get('transliteration_file_underscore_replacement_option', TRUE)) {

    // Replace underscore.
    $filename = str_replace('_', variable_get('transliteration_file_underscore_replacement', '_'), $filename);
  }

  // Remove remaining unsafe characters.
  $filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);

  // Remove multiple consecutive non-alphabetical characters.
  $filename = preg_replace('/(_)_+|(\\.)\\.+|(-)-+/', '\\1\\2\\3', $filename);

  // Force lowercase to prevent issues on case-insensitive file systems.
  if (variable_get('transliteration_file_lowercase', TRUE)) {
    $filename = drupal_strtolower($filename);
  }
  return $filename;
}