From b6390f959cdaf47676fd7a806d7f9a718fd6ab7e Mon Sep 17 00:00:00 2001 From: "Laurent Destailleur (aka Eldy)" Date: Thu, 23 Jan 2025 22:19:39 +0100 Subject: [PATCH] Add slugify function for security purpose --- htdocs/core/lib/functions.lib.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 459bc021b4d..e436c45cfb5 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -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 *