Skip to content

Instantly share code, notes, and snippets.

@arterm-sedov
Created December 12, 2024 12:04
Show Gist options
  • Select an option

  • Save arterm-sedov/2219fe01aaacb964cdead7dc843a5af0 to your computer and use it in GitHub Desktop.

Select an option

Save arterm-sedov/2219fe01aaacb964cdead7dc843a5af0 to your computer and use it in GitHub Desktop.
PHP code to convert Russian letters to Latin lower case letters
<?php
function transliterate_to_lower($string)
{
$cyr = ['а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п', 'р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я'];
$lat = ['a','b','v','g','d','e','e','zh','z','i','j','k','l','m','n','o','p','r','s','t','u','f','h','c','ch','sh','sh','','y','','e','yu','ya'];
$strlat = str_replace($cyr, $lat, mb_strtolower($string));
return $strlat;
}
echo transliterate_to_lower("Привет, 1-й ёжик!");
?>
@arterm-sedov
Copy link
Author

arterm-sedov commented Dec 12, 2024

Input: Привет, 1-й ёжик!
Output: privet, 1-j ezhik!

This code may be useful to preprocess Russian strings for slugification, e.g. in URLs.
It is based on GOST R 7.0.34-2014 with the following exceptions:

  • ё = e (instead of yo)
  • щ = sh (instead of shh)
  • ъ — omitted
  • ь — omitted

It works very differently from transliterator_transliterate() where:

  • ж = z
  • ч = c
  • ш = s
  • щ = s
  • Ъ = ``
  • Ь = `
  • ю = u
  • я = a

As a result, transliterator_transliterate() does not follow the typical Russian transliteration rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment