Slugify

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 29 mei 2019 om 14:06 (Jeroen Strompf heeft pagina Convert2slug (MySQL) hernoemd naar Slug (MySQL))
Naar navigatie springen Naar zoeken springen

Hoe fiets je een string om naar iets dat geschikt is als slug of bestandsnaam?

[1]:

CREATE FUNCTION toSlug(s NVARCHAR(500)) RETURNS NVARCHAR(500) DETERMINISTIC
 
RETURN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(LOWER(TRIM(s)), 
':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '/', ''), '"', ''), '?', ''),
"'", ''), '&', ''), '!', ''), '.', ''), ' ', '-'), '--', '-'), '--', '-'),'ù','u'),
'ú','u'),'û','u'),'ü','u'),'ý','y'),'ë','e'),'à','a'),'á','a'),'â','a'),'ã','a'), 
'ä','a'),'å','a'),'æ','a'),'ç','c'),'è','e'),'é','e'),'ê','e'),'ë','e'),'ì','i'),
'í','i'),'ě','e'), 'š','s'), 'č','c'),'ř','r'), 'ž','z'), 'î','i'),'ï','i'),'ð','o'),
'ñ','n'),'ò','o'),'ó','o'),'ô','o'),'õ','o'),'ö','o'),'ø','o'),'%', '');

[2]:

LOWER(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM('My String'), ':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\"', ''), '?', ''), '\'', ''), '&', ''), '!', ''), '.', ''), ' ', '-'), '--', '-'), '--', '-')) AS `post_name`

Mijn oplossing

Basically de eerste oplossing, inclusief een deel van de feedback in het artikel + eigen formattering:

create function slug(s nvarchar(500)) returns nvarchar(500) deterministic
 
return 

replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(lower(trim(s)), 
"'",	''),
' ',	'-'),
'!',	''),
'"',	''),
'%',	''),
'&',	''),
'(',	''),
')',	''),
',	',	''),
'--',	'-'),
'--',	'-'),
'.',	''),
'/',	''),
':',	''),
'?',	''),
'\\',	''),
'à',	'a'),
'á',	'a'),
'â',	'a'),
'ã',	'a'),
'ä',	'a'),
'å',	'a'),
'æ',	'ae'),
'ç',	'c'),
'è',	'e'),
'é',	'e'),
'ê',	'e'),
'ë',	'e'),
'ë',	'e'),
'ì',	'i'),
'í',	'i'),
'î',	'i'),
'ï',	'i'),
'ð',	'd'),
'ñ',	'n'),
'ò',	'o'),
'ó',	'o'),
'ô',	'o'),
'õ',	'o'),
'ö',	'o'),
'ø',	'o'),
'ù',	'u'),
'ú',	'u'),
'û',	'u'),
'ü',	'u'),
'ý',	'y'),
'č',	'c'),
'ě',	'e'),
'ř',	'r'),
'š',	's'),
'ž',	'z');

Ongetwijfeld is er ruimte voor nog meer vreemde symbolen. Daarnaast kan het misschien helpen om de input-string eerst om te fietsen naar een eenvoudigere karaktercodering. En het zou zelfs nog iteratief kunnen, maar dat lijkt me overkill


Zie ook

Bronnen