Skip to content

Instantly share code, notes, and snippets.

@quydm
Created May 6, 2017 03:23
Show Gist options
  • Save quydm/4b81a14c2364f46380142c002326d871 to your computer and use it in GitHub Desktop.
Save quydm/4b81a14c2364f46380142c002326d871 to your computer and use it in GitHub Desktop.
Rename songs
<?php
/*
* I have many .mp3 files with naming rule is: <song's name> - <singer's name> [MP3 320kbps].mp3. Ex: Dao Nguyen - NSUT Thuy Huong [MP3 320kbps].mp3
* I want to rename them using new naming rule is: <song's name>.mp3. Ex: Dao_Nguyen.mp3
*/
date_default_timezone_set('Asia/Ho_Chi_Minh');
$dir = "directory";
if (!is_dir($dir))
exit($dir . "is not a directory" . PHP_EOL);
$files = scandir($dir);
foreach ($files as $file) {
$file_path = $dir . $file;
if (is_file($file_path)) {
$pieces = explode("-", $file);
$new_name = str_replace(" ", "_", trim($pieces[0]));
rename($file_path, $dir . $new_name . ".mp3");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment