Add slugify function for security purpose

This commit is contained in:
Laurent Destailleur (aka Eldy) 2025-01-23 22:19:39 +01:00
parent db128814b5
commit b6390f959c

View File

@ -1874,6 +1874,33 @@ function dol_string_nounprintableascii($str, $removetabcrlf = 1)
}
}
/**
* Returns text slugified (no special char, separator is "-".
*
* @param string $stringtoslugify String to slugify
* @return string Slugified string
*/
function dolSlugify($stringtoslugify)
{
$slug = dol_string_unaccent($stringtoslugify);
// Convert special characters to their ASCII equivalents
if (function_exists('iconv')) {
$slug = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $slug);
}
// Convert to lowercase
$slug = strtolower($slug);
// Replace non-alphanumeric characters with hyphens
$slug = preg_replace('/[^a-z0-9]+/', '-', $slug);
// Remove leading and trailing hyphens
$slug = trim($slug, '-');
return $slug;
}
/**
* Returns text escaped for inclusion into javascript code
*