<?php
function translit($string){
$rus = array("/а/", "/б/", "/в/",
"/г/", "/ґ/", "/д/", "/е/", "/ё/", "/ж/",
"/з/", "/и/", "/й/", "/к/", "/л/", "/м/",
"/н/", "/о/", "/п/", "/р/", "/с/", "/т/",
"/у/", "/ф/", "/х/", "/ц/", "/ч/", "/ш/",
"/щ/", "/ы/", "/э/", "/ю/", "/я/", "/ь/",
"/ъ/", "/і/", "/ї/", "/є/", "/А/", "/Б/",
"/В/", "/Г/", "/ґ/", "/Д/", "/Е/", "/Ё/",
"/Ж/", "/З/", "/И/", "/Й/", "/К/", "/Л/",
"/М/", "/Н/", "/О/", "/П/", "/Р/", "/С/",
"/Т/", "/У/", "/Ф/", "/Х/", "/Ц/", "/Ч/",
"/Ш/", "/Щ/", "/Ы/", "/Э/", "/Ю/", "/Я/",
"/Ь/", "/Ъ/", "/І/", "/Ї/", "/Є/");
$lat = array("a", "b", "v",
"g", "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", "'", "'", "i",
"i", "e", "A", "B", "V", "G", "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", "'", "'", "I", "I", "E");
return preg_replace($rus, $lat, $string);
}
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
$text = translit($text);
echo $text . "\n";
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
echo slugify('Тестовый пример');
- Output for 4.4.0 - 4.4.9, 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.29 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
- Testovyj-primer
testovyj-primer
- Output for 4.3.11, 5.0.0 - 5.0.4
- Warning: preg_replace(): Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 3 in /in/ELZ6V on line 35
n-a
- Output for 4.3.0 - 4.3.10
- Warning: Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 3 in /in/ELZ6V on line 35
n-a
preferences:
191.45 ms | 401 KiB | 306 Q