string slugify code example
Example 1: java script converting text to slug
function convertToSlug(Text)
{
return Text
.toLowerCase()
.replace(/ /g,'-')
.replace(/[^\w-]+/g,'')
;
}
Example 2: php slugify function
function slugify($string, $replace = array(), $delimiter = '_')
{
if (!extension_loaded('iconv')) {
throw new Exception('iconv module not loaded');
}
$oldLocale = setlocale(LC_ALL, '0');
setlocale(LC_ALL, 'en_US.UTF-8');
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
if (!empty($replace)) {
$clean = str_replace((array) $replace, ' ', $clean);
}
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower($clean);
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
$clean = trim($clean, $delimiter);
setlocale(LC_ALL, $oldLocale);
return $clean;
}
echo slugify('àa kasdlnmkl asld amlks o+s df<+ùpsfjovù+p<sjrfàp');