Postgres | Function to remove HTML tags from text

|
| By Webner

Problem: How to create a function in Postgres that will remove HTML tags from a piece of text?

Solution: Create function in Postgres :

CREATE OR REPLACE FUNCTION strip_tags(TEXT) RETURNS TEXT AS $$
SELECT regexp_replace($1, ‘<[^>]*>’, ”, ‘g’)
$$ LANGUAGE SQL;

How to use:

SELECT strip_tags('<div>Kailash</div><b>Kumar</b>');

Output: KailashKumar

Note: This function will remove all the content between < and > symbol. If HTML tags are not proper then your text may also get removed so check your HTML before parsing it through this function.

Leave a Reply

Your email address will not be published. Required fields are marked *