Skip to content

Instantly share code, notes, and snippets.

@odirleiborgert
Forked from rafaelgou/StringUtil.php
Created September 3, 2013 13:25
Show Gist options
  • Save odirleiborgert/6423887 to your computer and use it in GitHub Desktop.
Save odirleiborgert/6423887 to your computer and use it in GitHub Desktop.
/**
* 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