Last active
October 8, 2015 09:47
-
-
Save Simounet/3314092 to your computer and use it in GitHub Desktop.
Slug class
This file contains 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 | |
class StrUtils { | |
static function slug($str){ | |
$str = strtolower($str); | |
$str = self::remove_accent($str); | |
return preg_replace(array('/[^a-zA-Z0-9 \'-]/', '/[ -\']+/', '/^-|-$/'), | |
array('', '-', ''), $str); | |
} | |
static function remove_accent($string) | |
{ | |
$a = array('à', 'á', 'â', 'ã', 'ä', 'å', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'è', 'é', | |
'ê', 'ë', 'ç', 'ì', 'í', 'î', 'ï', 'ù', 'ú', 'û', 'ü', 'ÿ', 'ñ'); | |
$b = array('a' ,'a' ,'a' ,'a' ,'a' ,'a' ,'o' ,'o' ,'o' ,'o' ,'o' ,'o' ,'e' ,'e' , | |
'e' ,'e' ,'c' ,'i' ,'i' ,'i' ,'i' ,'u' ,'u' ,'u' ,'u' ,'y' ,'n'); | |
return str_replace($a, $b, $string); | |
} | |
// alternative function | |
static function filter( $str ) { | |
$str = strtolower( iconv('UTF-8','ASCII//TRANSLIT//IGNORE', $str) ); | |
$search = array ('@[ ]@i','@[^a-zA-Z0-9_]@'); | |
$replace = array ('_',''); | |
return preg_replace($search, $replace, $str); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment