Skip to content

Instantly share code, notes, and snippets.

@nicocedron
Last active March 4, 2016 20:14
Show Gist options
  • Save nicocedron/982d933efc3c203b3641 to your computer and use it in GitHub Desktop.
Save nicocedron/982d933efc3c203b3641 to your computer and use it in GitHub Desktop.
Perfect Slug PHP
<?php
setlocale(LC_ALL, 'en_US.UTF8');
function toAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
$string = '€|@~@#¬½~[½[éááááééénico 22 aaaa';
echo toAscii($string);
//return
//eur-not-1-2-1-2-eaaaaeeenico-22-aaaa
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment