Skip to content

Instantly share code, notes, and snippets.

@Simounet
Last active October 8, 2015 09:47
Show Gist options
  • Save Simounet/3314092 to your computer and use it in GitHub Desktop.
Save Simounet/3314092 to your computer and use it in GitHub Desktop.
Slug class
<?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