Skip to content

Instantly share code, notes, and snippets.

@shadowlik
Forked from abn/slugify.postgres.sql
Created February 21, 2023 19:00
Show Gist options
  • Save shadowlik/17eb5e713db201d21c9ae000f9e66da7 to your computer and use it in GitHub Desktop.
Save shadowlik/17eb5e713db201d21c9ae000f9e66da7 to your computer and use it in GitHub Desktop.
A slugify function for postgres
-- 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