Slugify
(Doorverwezen vanaf Slug (MySQL))
Naar navigatie springen
Naar zoeken springen
How to convert a string to something that is suitable as a slug or file name?
This article covers MySQL and Bash.
MySQL
Example Ryadel.com
[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`
Sproc 'slug' - Without transliteration
Inspired by the example above.
- Note that it doesn't contain any transliteration. E.g.:
ö
is converted here too
, notoe
- I think I would prefer including transliteration: It's more accurate and reduces the change that two slugs will become identical.
CREATE DEFINER=`root`@`localhost` FUNCTION `slug`(s nvarchar(500)) RETURNS varchar(500) CHARSET utf8 DETERMINISTIC BEGIN # Convert a text string to a slug (within a certain interpretaton) ########################################################################################## # # * Without conversion to lowercase # * "Replace" replaces all instances of a given symbol - No need to replace a certain # symbol multiple times # * There's room for lots of other symbols. Maybe a shortcut: Convert the input string to # a more restricted character encoding first, and than filter the remaining stuff # * Source: http://wiki.devliegendebrigade.nl/Slug_(MySQL) # * Lost count of the number of braces? Just add or delete some, until the error # disappears ;) # return # Opening braces 1-50 ##################### # 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( # Remaining opening braces ########################## # replace(replace(replace( trim(s), # Do these as last (in the outer shells) ############################################## # # * To catch double dashes, earlier converted from string # * This works for up to 6 spaces in a row. If input strings can contain more than # 6 spaces in a row: Run this function multiple times on the same string # '--', '-'), '--', '-'), '--', '-'), # Now the usual stuff ############################################## # "'", ''), ' ', '-'), # Space to normal dash. Not to underscore '!', ''), '"', ''), '%', ''), '&', ''), '(', ''), ')', ''), ',', ''), # Controversial to eliminate commas? ', ', ''), '.', ''), '/', ''), ':', ''), '?', ''), '\\', ''), 'à', 'a'), 'á', 'a'), 'â', 'a'), 'ã', 'a'), 'ä', 'a'), 'å', 'a'), 'æ', 'ae'), 'ç', 'c'), # Occured in Summer 2019 in the wild: Curaçao 'è', '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'); END
Bash - With transliteration
WordPress - Native function?
When it's about generating a slug for a WordPress object, then why not tap into the native function for this. I'm sure there must be one, and it probably does something more: Make sure that the slug is unique.
See also
Sources
- https://www.ryadel.com/en/mysql-function-query-convert-string-slug-readable-url-permalinks/
- https://gist.github.com/matoakley/1092571
- https://stackoverflow.com/questions/16065288/easy-way-of-generating-a-slug-name-column-from-the-name-column
- https://developer.wordpress.org/reference/functions/wp_unique_term_slug/ - Related to taxonomies. Not to slugs in general