Slugify: verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
 
(12 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 1: Regel 1:
Hoe fiets je een string om naar iets dat geschikt is als slug of bestandsnaam?
+
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 ===
  
 
[https://www.ryadel.com/en/mysql-function-query-convert-string-slug-readable-url-permalinks/]:
 
[https://www.ryadel.com/en/mysql-function-query-convert-string-slug-readable-url-permalinks/]:
Regel 26: Regel 32:
 
</pre>
 
</pre>
  
== Mijn oplossing ==
+
=== Sproc 'slug' - Without transliteration ===
 +
 
 +
Inspired by the example above.
  
Met dank aan de eerste oplossing hierboven:
+
* Note that it doesn't contain any ''transliteration''. E.g.: <code>ö</code> is converted here to <code>o</code>, not <code>oe</code>
 +
* I think I would prefer including transliteration: It's more accurate and reduces the change that two slugs will become identical.
  
 
<pre>
 
<pre>
Regel 35: Regel 44:
 
BEGIN
 
BEGIN
  
# Convert a text string to a slug (within a certain interpretaton
+
# Convert a text string to a slug (within a certain interpretaton)
 
##########################################################################################
 
##########################################################################################
 
#
 
#
Regel 100: Regel 109:
 
'å', 'a'),
 
'å', 'a'),
 
'æ', 'ae'),
 
'æ', 'ae'),
'ç', 'c'),
+
'ç', 'c'),   # Occured in Summer 2019 in the wild: Curaçao
 
'è', 'e'),
 
'è', 'e'),
 
'é', 'e'),
 
'é', 'e'),
Regel 131: Regel 140:
 
</pre>
 
</pre>
  
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
+
== 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.
  
== Zie ook ==
+
== See also ==
  
 
* [[SEO]]
 
* [[SEO]]
 +
* [[Transliteratie]]
  
== Bronnen ==
+
== Sources ==
  
 
* https://www.ryadel.com/en/mysql-function-query-convert-string-slug-readable-url-permalinks/
 
* https://www.ryadel.com/en/mysql-function-query-convert-string-slug-readable-url-permalinks/
 
* https://gist.github.com/matoakley/1092571
 
* https://gist.github.com/matoakley/1092571
 
* https://stackoverflow.com/questions/16065288/easy-way-of-generating-a-slug-name-column-from-the-name-column
 
* 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

Huidige versie van 12 okt 2022 om 11:49

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 to o, not oe
  • 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