Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wrabit/878ac0bf02f215a0aa25444a7e2a9d20 to your computer and use it in GitHub Desktop.
Save wrabit/878ac0bf02f215a0aa25444a7e2a9d20 to your computer and use it in GitHub Desktop.
Simple URL Slugging Function with Transliteration
<?php
/**
* Slugifies a given string prepping it for usage as a URL string. Includes transliteration. Multi-byte ready for UTF-8.
* @param str $str Original url
* @param str $delimiter Separator to be used between words, default '-'
* @param bool $strtolower Keep existing case or convert all to lower, default true
* @return str Sluggified string;
*/
function slugify($str = '', $delimiter='-', $strtolower = true) {
$str = convert_foreign_characters($str);
$clean = preg_replace('/[^a-zA-Z0-9\/_|+ -]/', '', $clean);
$clean = trim($clean, '-');
if($strtolower) $clean = mb_strtolower($clean);
$clean = preg_replace('/[\/_|+ -]+/', $delimiter, $clean);
return $clean;
}
$translit_characters = array(
// Aa
'/Α|Ά|А|À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
'/α|ά|а|à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
// Bb
'/Β|Б/' => 'B',
'/β|б/' => 'b',
// Cc
'/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
'/ç|ć|ĉ|ċ|č/' => 'c',
// Dd
'/Δ|Д|Ð|Ď|Đ/' => 'D',
'/δ|д|ð|ď|đ/' => 'd',
// Ee
'/Ε|Έ|Е|Э|Є|È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E',
'/ε|έ|е|э|є|è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e',
// Ff
'/Φ|Ф/' => 'F',
'/φ|ф|ƒ/' => 'f',
// Gg
'/Γ|Г|Ĝ|Ğ|Ġ|Ģ/' => 'G',
'/γ|г|ĝ|ğ|ġ|ģ/' => 'g',
// Hh
'/Х|Ĥ|Ħ/' => 'H',
'/х|ĥ|ħ/' => 'h',
// Ii
'/Η|Ή|Ι|Ί|И|Ы|І|Ї|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й|Ý|Ÿ|Ŷ/' => 'Y',
'/υ|ύ|ψ|й|ý|ÿ|ŷ/' => 'y',
// Zz
'/Ζ|З|Ź|Ż|Ž/' => 'Z',
'/ζ|з|ź|ż|ž/' => 'z',
'/Θ/' => 'Th',
'/θ/' => 'th',
'/Ξ/' => 'Ks',
'/ξ/' => 'ks',
'/Ё/' => 'Yo',
'/ё/' => 'yo',
'/Ж/' => 'Zh',
'/ж/' => 'zh',
'/Ц/' => 'Ts',
'/ц/' => 'ts',
'/Ч/' => 'Ch',
'/ч/' => 'ch',
'/Ш/' => 'Sh',
'/ш/' => 'sh',
'/Щ/' => 'Sch',
'/щ/' => 'sch',
'/Ь|Ъ/' => '',
'/ь|ъ/' => '',
'/Ю/' => 'Yu',
'/ю/' => 'yu',
'/Я/' => 'Ya',
'/я/' => 'ya',
'/Æ|Ǽ/' => 'AE',
'/Ä/' => 'Ae',
'/ä|æ|ǽ/' => 'ae',
'/Œ/' => 'OE',
'/Ö/' => 'Oe',
'/ö|œ/' => 'oe',
'/Ü/' => 'Ue',
'/ü/' => 'ue',
'/IJ/' => 'IJ',
'/ij/' => 'ij',
'/ß/'=> 'ss',
);
/**
* Transliterate foreign characters to their latin counterparts
* @param str $str_i String to transliterate
* @return str Transliterated string
*/
function convert_foreign_characters($str_i) {
return @preg_replace(array_keys($translit_characters), array_values($translit_characters), $str_i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment