Skip to content

Instantly share code, notes, and snippets.

@matoakley
Created July 19, 2011 14:38
Show Gist options
  • Star 76 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save matoakley/1092571 to your computer and use it in GitHub Desktop.
Save matoakley/1092571 to your computer and use it in GitHub Desktop.
MySQL to convert a string into a slug
LOWER(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM('My String'), ':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\"', ''), '?', ''), '\'', ''), '&', ''), '!', ''), '.', ''), ' ', '-'), '--', '-'), '--', '-')) AS `post_name`
@dominiquevienne
Copy link

Read comments after made some fixes for my own purposes... but I think it could be relevant to split the replaces as done here since it will avoid having '_' at the beginning or the end of the slugged string

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(
		TRIM(		REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
				LOWER(rowToBeSlugged)
			, '·', ''), '|', ''), '®', ''), '`', ''), '°', ''), '$', ''), ':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\"', ''), '?', ''), '&', ''), '!', ''), '.', ''), '[', ''), ']', '')
		), '’', '-'), '\'', '-'), '–', '-'), ' ', '-'), '--', '-'), '--', '-'), 'ñ', 'n'), 'ç', 'c'), 'ø', 'oe'), 'ó', 'o'), 'ô', 'o'), 'ö', 'o'), 'î', 'i'), 'í', 'i'), 'ï', 'i'), 'ú', 'u'), 'ü', 'u'), 'û', 'u'), 'ù', 'u'), 'ý', 'y'), 'ë', 'e'), 'ê', 'e'), 'é', 'e'), 'è', 'e'), 'å', 'a'), 'â', 'a'), 'ä', 'a'), 'á', 'a'), 'à', 'a'), 'ã', 'a'), '²', '2'), '³', '3'), 'æ', 'ae'), 'œ', 'oe'), 'ḥ', 'h')

Let me know if it's useful for you

@dominiquevienne
Copy link

Forgot to mention a useful thing:

If you search for rows with slugged issues, you can use

WHERE rowName <> CONVERT(rowName USING ASCII)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment