PHP
1
Вклад в тег
<?php
class UrlTranslitBehavior{
protected static function u_strtr ($value, $to, $from = null)
{
if (is_null ($from))
{
arsort ($to, SORT_LOCALE_STRING);
foreach ($to as $c => $r)
{
$value = str_replace ($c, $r, $value);
}
}
else
{
$len = min (strlen ($to), strlen ($from));
for ($i = 0; $i < $len; ++$i)
{
$value = str_replace (
mb_substr ($to, $i, 1, 'UTF-8'),
mb_substr ($from, $i, 1, 'UTF-8'),
$value
);
}
}
return $value;
}
public static function translit ($value)
{
$value = trim ($value);
$value = str_replace (
array ("\r", "\n", "\t", ',', '.', '(', ')', '[', ']', '{', '}'),
'',
$value
);
if (!isset ($lang))
{
$regexpRus = '/^[а-яА-Я]+/';
$lang = preg_match ($regexpRus, $value) ? 'ru' : 'en';
}
// Сначала заменяем "односимвольные" фонемы.
$value = self::u_strtr ($value, "абвгдеёзийклмнопрстуфхыэ ", "abvgdeeziyklmnoprstufhie-");
$value = self::u_strtr ($value, "АБВГДЕЁЗИЙКЛМНОПРСТУФХЫЭ ", "ABVGDEEZIYKLMNOPRSTUFHIE-");
// Затем - "многосимвольные".
$value = self::u_strtr (
$value,
array (
"ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh",
"щ"=>"shch","ь"=>"", "ъ"=>"", "ю"=>"yu", "я"=>"ya",
"Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH",
"Щ"=>"SHCH","Ь"=>"", "Ъ"=>"", "Ю"=>"YU", "Я"=>"YA",
"ї"=>"i", "Ї"=>"Yi", "є"=>"ie", "Є"=>"Ye",
" "=>"-"
)
);
return $value;
}
public static function makeUrlLink ($value)
{
$link = self::translit ($value, 'en');
$link = preg_replace ('/([^0-9a-zA-Z_])+/', '-', $link);
$link = preg_replace ('/[_]{2,}/', '_', $link);
$link = preg_replace ('/^_/', '', $link);
$link = preg_replace ('/_$/', '', $link);
$link = trim($link,'-');
return strtolower($link);
}
}