Skip to content

Instantly share code, notes, and snippets.

@morontt
Created May 22, 2014 12:33
Show Gist options
  • Save morontt/3c171bdc8db6dba61714 to your computer and use it in GitHub Desktop.
Save morontt/3c171bdc8db6dba61714 to your computer and use it in GitHub Desktop.
example slug
<?php
namespace Puper\SuperBundle\Util;
class Slug
{
const CASE_ORIGIN = 1;
const CASE_LOWER = 1;
const CASE_UPPER = 2;
public function slugify($string, $toCase = self::CASE_LOWER)
{
$string = $this->translit($string, 'translit');
$string = trim($string);
$string = preg_replace('/\ +/', '-', $string);
$string = preg_replace('/[^a-zA-Z0-9\-]/', '', $string);
if ($toCase == self::CASE_LOWER) {
$string = mb_strtolower($string, 'UTF-8');
} elseif ($toCase == self::CASE_UPPER) {
$string = mb_strtoupper($string, 'UTF-8');
}
return trim($string);
}
public function translit($string)
{
$table = array(
'А' => 'A',
'Б' => 'B',
'В' => 'V',
'Г' => 'G',
'Д' => 'D',
'Е' => 'E',
'Ё' => 'YO',
'Ж' => 'ZH',
'З' => 'Z',
'И' => 'I',
'Й' => 'J',
'К' => 'K',
'Л' => 'L',
'М' => 'M',
'Н' => 'N',
'О' => 'O',
'П' => 'P',
'Р' => 'R',
'С' => 'S',
'Т' => 'T',
'У' => 'U',
'Ф' => 'F',
'Х' => 'H',
'Ц' => 'C',
'Ч' => 'CH',
'Ш' => 'SH',
'Щ' => 'CSH',
'Ь' => '',
'Ы' => 'Y',
'Ъ' => '',
'Э' => 'E',
'Ю' => 'YU',
'Я' => 'YA',
'а' => 'a',
'б' => 'b',
'в' => 'v',
'г' => 'g',
'д' => 'd',
'е' => 'e',
'ё' => 'yo',
'ж' => 'zh',
'з' => 'z',
'и' => 'i',
'й' => 'j',
'к' => 'k',
'л' => 'l',
'м' => 'm',
'н' => 'n',
'о' => 'o',
'п' => 'p',
'р' => 'r',
'с' => 's',
'т' => 't',
'у' => 'u',
'ф' => 'f',
'х' => 'h',
'ц' => 'c',
'ч' => 'ch',
'ш' => 'sh',
'щ' => 'csh',
'ь' => '',
'ы' => 'y',
'ъ' => '',
'э' => 'e',
'ю' => 'yu',
'я' => 'ya',
/*
* Ukrainian alphabet additions
*/
'Ґ' => 'G',
'ґ' => 'g',
'Є' => 'E',
'є' => 'e',
'І' => 'I',
'і' => 'i',
'Ї' => 'I',
'ї' => 'i'
);
$output = str_replace(
array_keys($table),
array_values($table),$string
);
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment