Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Rename *nix filenames


I am a bit slow sometimes and it took me a while to find this unix command that most are probably already aware of. rename is a very useful tool. The example I have here will susbtitute "foo" for "bar" of any file with foo in the name. The really nice thing about this is that it can use and regular expression syntax (ie. "^", "$", etc..).

rename -n 's/foo/bar/' *foo*

Return the extension from a file name

// Return the extension from a file name

/(.*\.)(.*$)/.match(File.basename(file_name))[2]

PHP filename bad character filter

// Function to filter out bad characters in a given filename

function replace_bad_filename_chars($filename) {
  $filtered_filename = "";

  $patterns = array(
    "/\s/", # Whitespace
    "/\&/", # Ampersand
    "/\+/"  # Plus
  );
  $replacements = array(
    "_",   # Whitespace
    "and", # Ampersand
    "plus" # Plus
  );
  
  $filename = preg_replace($patterns,$replacements,$filename);
  for ($i=0;$i<strlen($filename);$i++) {
    $current_char = substr($filename,$i,1);
    if (ctype_alnum($current_char) == TRUE || $current_char == "_" || $current_char == ".") {
      $filtered_filename .= $current_char;
    }
  }     
        
  return $filtered_filename;
}
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS