Skip to content

Instantly share code, notes, and snippets.

@rafaelgou
Last active December 22, 2015 05:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rafaelgou/6422801 to your computer and use it in GitHub Desktop.
Save rafaelgou/6422801 to your computer and use it in GitHub Desktop.
PHP Slug generator class/method
/**
* Description of StringUtil
*
* @author <rafaelgou@gmail.com> 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