Created
January 26, 2022 09:54
-
-
Save forsvunnet/2b877a4372981033c457e139536487ba to your computer and use it in GitHub Desktop.
Function to rename a file using snake case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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