Created
December 9, 2016 13:39
-
-
Save dlueth/0be0506ea849bc5b48c7f439ba4bb4a0 to your computer and use it in GitHub Desktop.
Sluggify in PHP
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
<?php | |
class Modify { | |
private static $instance = NULL; | |
protected static $mbstring = NULL; | |
protected static $iconv = NULL; | |
protected static $characterset = NULL; | |
/* | |
protected static $diacritics = array( | |
'À'=>'A','Á'=>'A','Â'=>'A','Ã'=>'A','Å'=>'A','Ä'=>'A','Æ'=>'AE', | |
'à'=>'a','á'=>'a','â'=>'a','ã'=>'a','å'=>'a','ä'=>'a','æ'=>'ae', | |
'Þ'=>'B','þ'=>'b','Č'=>'C','Ć'=>'C','Ç'=>'C','č'=>'c','ć'=>'c', | |
'ç'=>'c','Ď'=>'D','ð'=>'d','ď'=>'d','Đ'=>'Dj','đ'=>'dj','È'=>'E', | |
'É'=>'E','Ê'=>'E','Ë'=>'E','è'=>'e','é'=>'e','ê'=>'e','ë'=>'e', | |
'Ì'=>'I','Í'=>'I','Î'=>'I','Ï'=>'I','ì'=>'i','í'=>'i','î'=>'i', | |
'ï'=>'i','Ľ'=>'L','ľ'=>'l','Ñ'=>'N','Ň'=>'N','ñ'=>'n','ň'=>'n', | |
'Ò'=>'O','Ó'=>'O','Ô'=>'O','Õ'=>'O','Ø'=>'O','Ö'=>'O','Œ'=>'OE', | |
'ð'=>'o','ò'=>'o','ó'=>'o','ô'=>'o','õ'=>'o','ö'=>'o','œ'=>'oe', | |
'ø'=>'o','Ŕ'=>'R','Ř'=>'R','ŕ'=>'r','ř'=>'r','Š'=>'S','š'=>'s', | |
'ß'=>'ss','Ť'=>'T','ť'=>'t','Ù'=>'U','Ú'=>'U','Û'=>'U','Ü'=>'U', | |
'Ů'=>'U','ù'=>'u','ú'=>'u','û'=>'u','ü'=>'u','ů'=>'u','Ý'=>'Y', | |
'Ÿ'=>'Y','ý'=>'y','ý'=>'y','ÿ'=>'y','Ž'=>'Z','ž'=>'z' | |
); | |
*/ | |
private function __construct() { | |
self::$mbstring = extension_loaded('mbstring'); | |
self::$iconv = extension_loaded('iconv'); | |
self::$characterset = ini_get('default_charset'); | |
if(self::$mbstring !== true) { | |
throw new \Exception('Extension "mbstring" not loaded'); | |
} | |
if(self::$iconv !== true) { | |
throw new \Exception('Extension "iconv" not loaded'); | |
} | |
if(empty(self::$characterset)) { | |
throw new \Exception('no "default_charset" set'); | |
} | |
} | |
public static function transliterate($value, $characterset = NULL) { | |
if(self::$instance === NULL) { | |
self::$instance = new self(); | |
} | |
if($characterset === NULL) { | |
$characterset = self::$characterset; | |
} | |
return iconv(mb_detect_encoding($value, NULL, true), $characterset . '//IGNORE//TRANSLIT', trim($value)); | |
} | |
public static function sluggify($value) { | |
if(self::$instance === NULL) { | |
self::$instance = new self(); | |
} | |
//return trim(strtolower(preg_replace('/([^\w]|-)+/', '-', trim(strtr(str_replace('\'', '', trim($value)), self::$diacritics))))); | |
return strtolower(preg_replace('/([^\w0-9_]|-)+/', '-', iconv(mb_detect_encoding($value, 'auto', true), 'ASCII//TRANSLIT//IGNORE', trim($value)))); | |
} | |
} | |
?> |
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
<?php | |
// include/require/load slup.php | |
ini_set('default_charset', 'utf-8'); | |
echo Modify::sluggify('Gnarf äüö? hützli pötzli') .chr(10) . '<br />'; | |
die(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment