I have got a requirement to remove entire text between<img and /img> from the text given with html tags without touching any other tags or text. Need to replace <img..../img> with space only to make sure all other tags will not break.
Create a table and insert the given text
use work
go
create table dbo.[text](
text varchar(max) not null
)
insert into text(text)
values('<br>formatted text to preserve <span class="mruText" style="color: rgb(51, 52, 53); font-family: Arial, Helvetica, sans-serif; background-color: rgb(207, 238, 248);"><a class="accountMru" href="https://na4.salesforce.com/001600000012345" style="color: rgb(51, 52, 53); font-family: Arial, Helvetica, sans-serif; background-color: rgb(207, 238, 248);" title="[Alt+1]" target="_blank">link text to linked account</a> </span>(Merged)
<img alt="alt text" height="375" src="https://c.na53.content.force.com/servlet/rtaImage?eid=001600000012345&feoid=00N600000012345&refid=0EM600000001234" width="500"></img><a href=www.test.com>testwording</a>')
,('<br>formatted text<br><img src="http://www.externalwebsite.com/img/ c6267244.jpg"></img><br>formatted text<br><br>formatted text<br>')
select * from text
Create function using below code.
CREATE FUNCTION [dbo].[udf_CutImageTags] (@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Start INT;
DECLARE @End INT;
DECLARE @Length INT;
SET @Start = CHARINDEX('<img', @HTMLText);
SET @End = CHARINDEX('img>', @HTMLText, CHARINDEX('<img', @HTMLText));
SET @Length = (@End - @Start) + 4;
WHILE @Start > 0
AND @End > 0
AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText, @Start, @Length, '');
SET @Start = CHARINDEX('img<', @HTMLText);
SET @End = CHARINDEX('img>', @HTMLText, CHARINDEX('<img', @HTMLText));
SET @Length = (@End - @Start) + 4;
END;
RETURN LTRIM(RTRIM(@HTMLText));
END;
Create a table and insert the given text
use work
go
create table dbo.[text](
text varchar(max) not null
)
insert into text(text)
values('<br>formatted text to preserve <span class="mruText" style="color: rgb(51, 52, 53); font-family: Arial, Helvetica, sans-serif; background-color: rgb(207, 238, 248);"><a class="accountMru" href="https://na4.salesforce.com/001600000012345" style="color: rgb(51, 52, 53); font-family: Arial, Helvetica, sans-serif; background-color: rgb(207, 238, 248);" title="[Alt+1]" target="_blank">link text to linked account</a> </span>(Merged)
<img alt="alt text" height="375" src="https://c.na53.content.force.com/servlet/rtaImage?eid=001600000012345&feoid=00N600000012345&refid=0EM600000001234" width="500"></img><a href=www.test.com>testwording</a>')
,('<br>formatted text<br><img src="http://www.externalwebsite.com/img/ c6267244.jpg"></img><br>formatted text<br><br>formatted text<br>')
select * from text
Create function using below code.
CREATE FUNCTION [dbo].[udf_CutImageTags] (@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Start INT;
DECLARE @End INT;
DECLARE @Length INT;
SET @Start = CHARINDEX('<img', @HTMLText);
SET @End = CHARINDEX('img>', @HTMLText, CHARINDEX('<img', @HTMLText));
SET @Length = (@End - @Start) + 4;
WHILE @Start > 0
AND @End > 0
AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText, @Start, @Length, '');
SET @Start = CHARINDEX('img<', @HTMLText);
SET @End = CHARINDEX('img>', @HTMLText, CHARINDEX('<img', @HTMLText));
SET @Length = (@End - @Start) + 4;
END;
RETURN LTRIM(RTRIM(@HTMLText));
END;
Execute this function by passing values from table text and compare the results.
SELECT dbo.udf_CutImageTags2([text]) FROM dbo.text
Let me know if it works for you as well.
No comments:
Post a Comment