Skip to content

Instantly share code, notes, and snippets.

@Black-Milk
Created August 2, 2024 12:25
Show Gist options
  • Save Black-Milk/1a23bac33bef9c6303d4ada443e8b161 to your computer and use it in GitHub Desktop.
Save Black-Milk/1a23bac33bef9c6303d4ada443e8b161 to your computer and use it in GitHub Desktop.
Postgres HTML Tag Remover
CREATE OR REPLACE FUNCTION REMOVE_HTML_TAGS(TEXT) RETURNS TEXT AS
$$
DECLARE
INPUT_TEXT ALIAS FOR $1;
CLEAN_TEXT TEXT;
BEGIN
-- Remove Tags (e.g. <p>, <a>, <div>)
CLEAN_TEXT := REGEXP_REPLACE(INPUT_TEXT, '<[^>]+>', '', 'g');
-- Remove entities (e.g. &nbsp;)
CLEAN_TEXT := REGEXP_REPLACE(CLEAN_TEXT,
'&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});',
'', 'g');
RETURN CLEAN_TEXT;
END;
$$ LANGUAGE PLPGSQL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment