Last active
June 10, 2021 07:03
-
-
Save sagittaracc/8b7a5e287bd02c3052d48d2eb38a6f41 to your computer and use it in GitHub Desktop.
Транслитерировать все имена файлов в указанной директории
This file contains 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 translit_folder($folder) | |
{ | |
$converter = array( | |
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', | |
'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', | |
'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', | |
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', | |
'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch', | |
'ш' => 'sh', 'щ' => 'sch', 'ь' => '', 'ы' => 'y', 'ъ' => '', | |
'э' => 'e', 'ю' => 'yu', 'я' => 'ya', | |
'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', | |
'Е' => 'E', 'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I', | |
'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', | |
'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', | |
'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch', | |
'Ш' => 'Sh', 'Щ' => 'Sch', 'Ь' => '', 'Ы' => 'Y', 'Ъ' => '', | |
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', '.' => '', | |
); | |
foreach (glob("$folder/*.*") as $filename) { | |
$new = ''; | |
$file = pathinfo(trim($filename)); | |
if (!empty($file['dirname']) && @$file['dirname'] != '.') { | |
$new .= rtrim($file['dirname'], '/') . '/'; | |
} | |
if (!empty($file['filename'])) { | |
$file['filename'] = str_replace(array(' ', ','), '-', $file['filename']); | |
$file['filename'] = strtr($file['filename'], $converter); | |
$file['filename'] = mb_ereg_replace('[-]+', '-', $file['filename']); | |
$file['filename'] = trim($file['filename'], '-'); | |
$new .= $file['filename']; | |
} | |
if (!empty($file['extension'])) { | |
$new .= '.' . $file['extension']; | |
} | |
rename($filename, $new); | |
} | |
} | |
translit_folder('./pics'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment