下面是一個快速和骯髒的解決方案,只使用T-SQL。
Rextester演示:http://rextester.com/TLF34624
CREATE FUNCTION processHtml(@html VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @remainingHtml VARCHAR(MAX),
@processedHtml VARCHAR(MAX),
@imgPos INT,
@closingAngleBracketPos INT,
@openingAngleBracketPos INT;
SET @remainingHtml = @html;
SET @imgPos = PATINDEX('%<img%[^</]>%', @remainingHtml);
WHILE @imgPos > 0
BEGIN
SET @processedHtml = CONCAT(@processedHtml, SUBSTRING(@remainingHtml, 1, @imgPos - 1));
SET @remainingHtml = SUBSTRING(@remainingHtml, @imgPos, LEN(@remainingHtml));
SET @closingAngleBracketPos = PATINDEX('%[^/]>%', @remainingHtml);
SET @openingAngleBracketPos = CHARINDEX('<', @remainingHtml, 2);
IF @closingAngleBracketPos < 0
SET @imgPos = -1;
ELSE IF @closingAngleBracketPos < @openingAngleBracketPos
BEGIN
SET @processedHtml = CONCAT(@processedHtml, SUBSTRING(@remainingHtml, 1, @closingAngleBracketPos), '/>');
SET @remainingHtml = SUBSTRING(@remainingHtml, @closingAngleBracketPos + 2, LEN(@remainingHtml));
SET @imgPos = PATINDEX('%<img%[^</]>%', @remainingHtml);
END
ELSE
BEGIN
SET @processedHtml = CONCAT(@processedHtml, SUBSTRING(@remainingHtml, 1, @openingAngleBracketPos - 1));
SET @remainingHtml = SUBSTRING(@remainingHtml, @openingAngleBracketPos, LEN(@remainingHtml));
SET @imgPos = PATINDEX('%<img%[^</]>%', @remainingHtml);
END
END
SET @processedHtml = CONCAT(@processedHtml, @remainingHtml);
RETURN @processedHtml;
END
謝謝,這是我一直在尋找 –
將獎勵在2個小時的賞金,當系統允許它 –