Created
March 29, 2012 14:04
-
-
Save dknight/2237757 to your computer and use it in GitHub Desktop.
Convert Russian letters to Latin
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 | |
/** | |
* Converts Russian letters to Latin | |
* | |
* Usage: | |
* <code> | |
* Russian::transliterate('Привет, как дела?'); | |
* Russian::transliterateUrl('Привет, как дела?'); | |
* </code> | |
* | |
* @package default | |
* @author Dmitri Smirnov | |
*/ | |
class Russian | |
{ | |
/** | |
* Converts text | |
* | |
* @param string $str | |
* @return string | |
*/ | |
public static function transliterate($str) | |
{ | |
$tr = array( | |
"А"=>"A","Б"=>"B","В"=>"V","Г"=>"G", | |
"Д"=>"D","Е"=>"E", "Ё"=>'E',"Ж"=>"J","З"=>"Z","И"=>"I", | |
"Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N", | |
"О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T", | |
"У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH", | |
"Ш"=>"SH","Щ"=>"SCH","Ъ"=>"","Ы"=>"YI","Ь"=>"", | |
"Э"=>"E","Ю"=>"YU","Я"=>"YA","а"=>"a","б"=>"b", | |
"в"=>"v","г"=>"g","д"=>"d","е"=>"e", "ё"=>"e","ж"=>"j", | |
"з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l", | |
"м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r", | |
"с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h", | |
"ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y", | |
"ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya" | |
); | |
return strtr($str,$tr); | |
} | |
/** | |
* Convert to URL slug like 'skidki-na-tovaryi' | |
* | |
* @param string $urlstr | |
* @return string | |
*/ | |
public static function transliterateUrl($urlstr) { | |
if (preg_match('/[^A-Za-z0-9_\-]/', $urlstr)) { | |
$urlstr = self::transliterate($urlstr); | |
$urlstr = preg_replace('/[^A-Za-z0-9]+/', '-', $urlstr); | |
} | |
return $urlstr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment