Other

From SUNY Potsdam ACM
Jump to: navigation, search
  1. PHP function to sanitize names of movie torrent file names.
  2. Author: Edward Banner

<?php

function sanitize_movie($torrent_movie) {

 // Remove everything inside of brackets
 $torrent_movie = preg_replace('/\[.*?\]/', '[]', $torrent_movie) . "\n";
 // Remove everything inside of curley braces
 $torrent_movie = preg_replace('/\{.*?\}/', '{}', $torrent_movie) . "\n";
 // Replace all weird punctuation characters with space characters
 $torrent_movie = preg_replace('/[()\{\}_\[\],-.]/', ' ', $torrent_movie) . "\n";
 // Trim off leading and trailing whitespace.
 $torrent_movie = trim($torrent_movie);
 // Split the string on whitespace.
 $tokens = preg_split('/\s+/', $torrent_movie);
 foreach ($tokens as $i => $token) {
   // Chop off every token after one of these
   if (preg_match('/([\d]{4})|(dvd)|(rip)|(limited)|(720p)|(h264)/i', $token)) {
     $sanitized = array_slice($tokens, 0, $i);
     break;
   }
 }
 if (! isset($sanitized))
   $sanitized = $tokens;
 // Pop off the last token if it's a codec extension.
 if (preg_match('/(mkv)|(avi)|(mp3)|(wmv)|(flv)/i', end($sanitized)))
   array_pop($sanitized);
 return implode(' ', $sanitized);

}

?>

Personal tools