Skip to content

Instantly share code, notes, and snippets.

@forsvunnet
Created January 26, 2022 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forsvunnet/2b877a4372981033c457e139536487ba to your computer and use it in GitHub Desktop.
Save forsvunnet/2b877a4372981033c457e139536487ba to your computer and use it in GitHub Desktop.
Function to rename a file using snake case
<?php
function rename_file_with_snake_case(string $filename): string {
$basename = basename($filename);
$dir = dirname($filename);
$newName = strtolower($basename);
$newName = preg_replace('/[^\w\.]/', ' ', $newName);
$newName = preg_replace('/\s+/', ' ', $newName);
$newName = preg_replace('/\s/', '_', $newName);
$newName = preg_replace('/_\./', '.', $newName);
rename($filename, "$dir/$newName");
return "$dir/$newName";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment