-
-
Save lvidal1/d1d72f14eea5485c74616d2250324ab2 to your computer and use it in GitHub Desktop.
Make a slug in PostgreSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- From http://scottbarnham.com/blog/2010/12/20/make-a-slug-in-postgresql-translating-diacritics/ | |
CREATE OR REPLACE FUNCTION getslug(texte varchar) RETURNS VARCHAR AS | |
$$ | |
DECLARE | |
result varchar; | |
BEGIN | |
-- Add formtat to special case | |
result := replace(texte , 'æ', 'ae'); | |
result := replace(result , 'œ', 'oe'); | |
result := replace(result , '€', 'euros'); | |
result := replace(result , '$', 'dollars'); | |
result := replace(result , '£', 'pound'); | |
result := replace(result , '¥', 'yen'); | |
result := replace(result , 'ño', 'nio'); | |
result := replace(result , 'ÑO', 'nio'); | |
result := replace(result , 'Ño', 'nio'); | |
result := replace(result , 'ñO', 'nio'); | |
result := regexp_replace(translate(replace(lower(result), ' ', '-'), | |
'áàâãäåāăąÁÂÃÄÅĀĂĄèééêëēĕėęěĒĔĖĘĚìíîïìĩīĭÌÍÎÏÌĨĪĬóôõöōŏőÒÓÔÕÖŌŎŐùúûüũūŭůÙÚÛÜŨŪŬŮçÇÿ&,.ñÑ', | |
'aaaaaaaaaaaaaaaaaeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiooooooooooooooouuuuuuuuuuuuuuuuccy_--nn'), E'[^\\w -]', '', 'g'); | |
RETURN result; | |
END; | |
$$ | |
LANGUAGE PLPGSQL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment