Last active
December 22, 2015 05:18
-
-
Save rafaelgou/6422801 to your computer and use it in GitHub Desktop.
PHP Slug generator class/method
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
/** | |
* Description of StringUtil | |
* | |
* @author <[email protected]> Rafael Goulart | |
*/ | |
class StringUtil { | |
/** | |
* Slugify a text and remove accents | |
* | |
* @param string $text The text. | |
* @param string $separator The separator | |
* | |
* @return string The text slugified. | |
*/ | |
static public function slugify($text, $separator='-') | |
{ | |
// remove accents | |
$from = 'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËẼÌÍÎÏĨÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëẽìíîïĩðñòóôõöøùúûüýÿ'; | |
$to = 'SOZsozYYuAAAAAAACEEEEEIIIIIDNOOOOOOUUUUYsaaaaaaaceeeeeiiiiionoooooouuuuyy'; | |
$text = utf8_decode($text); | |
$text = strtr($text, utf8_decode($from), $to); | |
// replace all non letters or digits by - | |
$text = preg_replace('/\W+/', $separator, $text); | |
// trim and lowercase | |
$text = strtolower(trim($text, $separator)); | |
return $text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment