Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //traverse a directory of images and clean up the filenames
- $image_dir = '/path/to/dir/';
- if($handle = opendir($image_dir)){
- while(false !== ($file_name = readdir($handle))){
- if($file_name == '.' || $file_name == '..'){ continue; }
- preg_match('/(.*)(\.\w+)$/', $file_name, $matches);
- $image_name = $matches[1];
- $image_extension = $matches[2];
- $new_name = preg_replace('/\-{2,}/', '-', preg_replace('/\W/', '-', strtolower($image_name)));
- rename("{$image_dir}{$file_name}", "{$image_dir}{$new_name}{$image_extension}");
- }
- closedir($handle);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement