Re: Function To Strip HTML - Mailing list pgsql-sql

From Ozer, Pam
Subject Re: Function To Strip HTML
Date
Msg-id 216FFB77CBFAEE4B8EE4DF0A939FF1D14F84CA@mail-001.corp.automotive.com
Whole thread Raw
In response to Function To Strip HTML  ("Ozer, Pam" <pozer@automotive.com>)
List pgsql-sql

I found a way to do this using regular expressions.  Found this on another website

 

CREATE OR REPLACE FUNCTION strip_tags(TEXT) RETURNS TEXT AS $$
2    SELECT regexp_replace(regexp_replace($1, E'(?x)<[^>]*?(\s alt \s* = \s* ([\'"]) ([^>]*?) \2) [^>]*? >', E'\3'), E'(?x)(< [^>]*? >)', '', 'g')
3$$ LANGUAGE SQL;

 

 

From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Ozer, Pam
Sent: Wednesday, February 23, 2011 3:41 PM
To: pgsql-sql@postgresql.org
Subject: [SQL] Function To Strip HTML

 

I have the following function that I used in MSSQL.  I would like to create the same function in PostgreSql.   I think I am a bit confused on how to create and set variables in PostgreSql.  Can someone give me a place to start to create something like this? 

 

 

Thanks

 

Pam

 

SET ANSI_NULLS ON

GO

 

SET QUOTED_IDENTIFIER ON

GO

 

CREATE FUNCTION [dbo].[udf_StripHTML]

(@HTMLText VARCHAR(MAX))

RETURNS VARCHAR(MAX)

AS

BEGIN

DECLARE @Start INT

DECLARE @End INT

DECLARE @Length INT

SET @Start = CHARINDEX('<',@HTMLText)

SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))

SET @Length = (@End - @Start) + 1

WHILE @Start > 0

AND @End > 0

AND @Length > 0

BEGIN

SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')

SET @Start = CHARINDEX('<',@HTMLText)

SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))

SET @Length = (@End - @Start) + 1

END

RETURN LTRIM(RTRIM(@HTMLText))

END

 

GO

 

pgsql-sql by date:

Previous
From: "Plugge, Joe R."
Date:
Subject: Re: Compare two Data bases Structure
Next
From: Piotr Czekalski
Date:
Subject: Re: Compare two Data bases Structure