Last active
October 12, 2015 23:38
-
-
Save mvar/4104870 to your computer and use it in GitHub Desktop.
Makes slug from text. I.e. for SEO friendly URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Makes SEO URL from string | |
| * | |
| * @author Mantas Varatiejus <var.mantas@gmail.com> | |
| * @copyright 2011-2013, Mantas Varatiejus | |
| */ | |
| class SlugGenerator | |
| { | |
| /** | |
| * Returns string with non-Latin characters replaced to Latin and then | |
| * any non-Latin replaced to "-" | |
| * | |
| * @param string $string | |
| * @param bool $lowerCase Returns lowercased string if TRUE | |
| * | |
| * @return string | |
| * | |
| * @assert ("Opinion: Why early electric vehicle adopters deserve...") == "Opinion-Why-early-electric-vehicle-adopters-deserve" | |
| * @assert ("Opinion: Why early electric vehicle adopters deserve...", true) == "opinion-why-early-electric-vehicle-adopters-deserve" | |
| * @assert ("Pusamžio svyravimas: Metų laikai keičia atomų būdą") == "Pusamzio-svyravimas-Metu-laikai-keicia-atomu-buda" | |
| */ | |
| public function getSlug($string, $lowerCase = false) | |
| { | |
| $string = $this->toLatin($string); | |
| if ($lowerCase) { | |
| $string = strtolower($string); | |
| } | |
| // replace non-latin letters to "-" | |
| $string = preg_replace('/[^A-Za-z0-9]+/', '-', $string); | |
| // remove "-" from the beginning and end | |
| $string = preg_replace('/(^[-]+)|([.]*[-]$)/', '', $string); | |
| return $string; | |
| } | |
| /** | |
| * Returns string with non-Latin characters replaced to Latin | |
| * | |
| * @param string $string | |
| * @return string | |
| */ | |
| public function toLatin($string) | |
| { | |
| $charsMap = $this->getCharactersMap(); | |
| foreach ($charsMap as $to => $from) { | |
| $string = preg_replace("/(" . implode('|', $from) . ")/u", $to, $string); | |
| foreach ($from as &$value) { | |
| $value = mb_strtolower($value, 'UTF-8'); | |
| } | |
| $string = preg_replace("/(" . implode('|', $from) . ")/u", strtolower($to), $string); | |
| } | |
| return $string; | |
| } | |
| /** | |
| * Returns map of characters to replace | |
| * | |
| * @return array | |
| */ | |
| protected function getCharactersMap() | |
| { | |
| return array( | |
| 'A' => array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ā', 'Ă', 'Ą', 'Ǎ', 'Ǻ'), | |
| 'AE' => array('Æ', 'Ǽ'), | |
| 'C' => array('Ç', 'Ć', 'Ċ', 'Ĉ', 'Č'), | |
| 'D' => array('Ð', 'Ď', 'Đ'), | |
| 'E' => array('È', 'É', 'Ê', 'Ë', 'Ē', 'Ĕ', 'Ė', 'Ę', 'Ě'), | |
| 'F' => array('ƒ'), | |
| 'G' => array('Ĝ', 'Ğ', 'Ġ', 'Ģ'), | |
| 'H' => array('Ĥ', 'Ħ'), | |
| 'I' => array('Ì', 'Í', 'Î', 'Ï', 'Ĩ', 'Ī', 'Ĭ', 'Į', 'İ', 'Ǐ'), | |
| 'IJ' => array('IJ'), | |
| 'J' => array('Ĵ'), | |
| 'K' => array('Ķ'), | |
| 'L' => array('Ĺ', 'Ļ', 'Ľ', 'Ŀ', 'Ł'), | |
| 'N' => array('Ñ', 'Ń', 'Ņ', 'Ň', 'ʼn'), | |
| 'O' => array('Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ō', 'Ŏ', 'Ő', 'Ơ', 'Ǒ', 'Ǿ', 'Ø'), | |
| 'OE' => array('Œ'), | |
| 'R' => array('Ŕ', 'Ŗ', 'Ř'), | |
| 'S' => array('Ś', 'Ŝ', 'Ş', 'Š', 'ſ'), | |
| 'SS' => array('ß'), | |
| 'T' => array('Ţ', 'Ť', 'Ŧ'), | |
| 'U' => array('Ù', 'Ú', 'Û', 'Ü', 'Ũ', 'Ū', 'Ŭ', 'Ů', 'Ű', 'Ų', 'Ư', 'Ǔ', 'Ǖ', 'Ǘ', 'Ǚ', 'Ǜ'), | |
| 'W' => array('Ŵ'), | |
| 'Y' => array('Ý', 'Ŷ', 'Ÿ'), | |
| 'Z' => array('Ź', 'Ż', 'Ž'), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment