Transliterator.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace app\helpers;
  3. class Transliterator
  4. {
  5. public static function convert(string $string):string
  6. {
  7. $converter = array(
  8. 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
  9. 'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
  10. 'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
  11. 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
  12. 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
  13. 'ш' => 'sh', 'щ' => 'sch', 'ь' => '', 'ы' => 'y', 'ъ' => '',
  14. 'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
  15. 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
  16. 'Е' => 'E', 'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I',
  17. 'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N',
  18. 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T',
  19. 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch',
  20. 'Ш' => 'Sh', 'Щ' => 'Sch', 'Ь' => '', 'Ы' => 'Y', 'Ъ' => '',
  21. 'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
  22. );
  23. $value = strtr($string, $converter);
  24. return $value;
  25. }
  26. public static function toUrl(string $string):string
  27. {
  28. $translit = self::convert($string);
  29. return strtolower(preg_replace("/([^a-zA-Z0-9-])/","",trim(str_replace(" ",'-',$translit))));
  30. }
  31. }