CREATE OR REPLACE FUNCTION slugify(input text) RETURNS text AS $$ BEGIN input := unaccent(input); input := lower(input); -- remove single and double quotes input := regexp_replace(input, '[''"]+', '', 'gi'); -- replace anything that's not a letter, number, hyphen, or underscore with a hyphen input := regexp_replace(input, '[^a-z0-9\-_]+', '-', 'gi'); -- trim hyphens if they exist on the ends of the string input := regexp_replace(input, '\-+$', ''); input := regexp_replace(input, '^\-', ''); RETURN input; END; $$ LANGUAGE plpgsql STRICT IMMUTABLE PARALLEL SAFE;