Created
March 9, 2012 15:59
-
-
Save h2rd/2007234 to your computer and use it in GitHub Desktop.
Translite with php
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 slugify($text) { | |
return strtolower(strtr(trim($text), array(' '=>'_') )); | |
} | |
function translit($str){ | |
static $tbl= array( | |
'а'=>'a', 'б'=>'b', 'в'=>'v', 'г'=>'g', 'д'=>'d', 'е'=>'e', 'ж'=>'g', 'з'=>'z', | |
'и'=>'i', 'й'=>'y', 'к'=>'k', 'л'=>'l', 'м'=>'m', 'н'=>'n', 'о'=>'o', 'п'=>'p', | |
'р'=>'r', 'с'=>'s', 'т'=>'t', 'у'=>'u', 'ф'=>'f', 'ы'=>'i', 'э'=>'e', 'А'=>'A', | |
'Б'=>'B', 'В'=>'V', 'Г'=>'G', 'Д'=>'D', 'Е'=>'E', 'Ж'=>'G', 'З'=>'Z', 'И'=>'I', | |
'Й'=>'Y', 'К'=>'K', 'Л'=>'L', 'М'=>'M', 'Н'=>'N', 'О'=>'O', 'П'=>'P', 'Р'=>'R', | |
'С'=>'S', 'Т'=>'T', 'У'=>'U', 'Ф'=>'F', 'Ы'=>'I', 'Э'=>'E', 'ё'=>"yo", 'х'=>"h", | |
'ц'=>"ts", 'ч'=>"ch", 'ш'=>"sh", 'щ'=>"shch", 'ъ'=>"", 'ь'=>"", 'ю'=>"yu", 'я'=>"ya", | |
'Ё'=>"YO", 'Х'=>"H", 'Ц'=>"TS", 'Ч'=>"CH", 'Ш'=>"SH", 'Щ'=>"SHCH", 'Ъ'=>"", 'Ь'=>"", | |
'Ю'=>"YU", 'Я'=>"YA",'і'=>'i','ї'=>'ji','І'=>'I', 'Ї'=>"Ji", '\''=>'' | |
); | |
return strtr($str, $tbl); | |
} | |
$d = dir("."); | |
$arr=array(); | |
while (false !== ($entry = $d->read())) | |
$arr[$entry]=slugify(translit($entry)); | |
foreach ($arr as $key => $value) { | |
if ( ($key==".") || ($key=="..") || ($key=="1.php") ) continue; | |
if ($key!=$value) rename($key,$value); | |
} | |
$d->close(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment