-
-
Save shadowlik/17eb5e713db201d21c9ae000f9e66da7 to your computer and use it in GitHub Desktop.
A slugify function for postgres
This file contains 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
-- original source: https://medium.com/adhawk-engineering/using-postgresql-to-generate-slugs-5ec9dd759e88 | |
-- https://www.postgresql.org/docs/9.6/unaccent.html | |
CREATE EXTENSION IF NOT EXISTS unaccent; | |
-- create the function in the public schema | |
CREATE OR REPLACE FUNCTION public.slugify( | |
v TEXT | |
) RETURNS TEXT | |
LANGUAGE plpgsql | |
STRICT IMMUTABLE AS | |
$function$ | |
BEGIN | |
-- 1. trim trailing and leading whitespaces from text | |
-- 2. remove accents (diacritic signs) from a given text | |
-- 3. lowercase unaccented text | |
-- 4. replace non-alphanumeric (excluding hyphen, underscore) with a hyphen | |
-- 5. trim leading and trailing hyphens | |
RETURN trim(BOTH '-' FROM regexp_replace(lower(unaccent(trim(v))), '[^a-z0-9\\-_]+', '-', 'gi')); | |
END; | |
$function$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment