Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Last active December 3, 2018 11:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mikulas/a5b95be4e4858c63b332 to your computer and use it in GitHub Desktop.
Save Mikulas/a5b95be4e4858c63b332 to your computer and use it in GitHub Desktop.
PostgreSQL UTF-8 to ASCII folding and webalize
CREATE EXTENSION unaccent;
CREATE OR REPLACE FUNCTION webalize(varchar) RETURNS text AS $$
SELECT trim(both '-' from regexp_replace(lower(unaccent($1)), '[^a-z0-9]+', '-', 'g'));
$$ LANGUAGE SQL;
SELECT unaccent('Příliš žlutý kůň 91. stupeň!');
-- Prilis zluty kun 91. stupen!
SELECT webalize('Příliš žlutý kůň 91. stupeň!');
-- prilis-zluty-kun-91-stupen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment