如何替换 <a href>.. 字符串中的 URL 文本?
How to replace URL text in <a href>.. string?
我的 SQL Server 2012 的一些数据在数据类型 nvarchar(max)
的列 comments
中。
示例数据:
For more information, please visit www.sample.com
If you desire submit a request please go here: www.request.com
Customer Service.
我需要得到这个:
For more information, please visit <a href="www.sample.com">www.sample.com</a>
If you desire submit a request please go here: <a href="www.request.com">www.request.com</a>
Customer Service."
所有链接都以 www
开头。感谢您的帮助
您可以创建这样的函数,接收原始字符串,然后返回结果 w/ html:
CREATE FUNCTION dbo.ufnUrlHref (
@str nvarchar(max)
)
RETURNS nvarchar(max)
AS
BEGIN
DECLARE @nextMatch int = patindex('%www.%', @str); --find start of next 'www.'
DECLARE @result nvarchar(max) = '';
WHILE (@nextMatch != 0)
BEGIN
DECLARE @matchEnd int = charindex(' ', @str, @nextMatch);
DECLARE @strLen int = len(@str);
DECLARE @first nvarchar(max) = substring(@str, 1, @strLen - (@strLen - @matchEnd)); --get all of string up to first url
DECLARE @last nvarchar(max) = substring(@str, @matchEnd + 1, @strLen - @matchEnd); --get rest of string after first url
DECLARE @url nvarchar(255) = substring(@str, @nextMatch, @matchEnd - @nextMatch); --get url
SET @first = replace(@first, @url, '<a href="' + @url + '">' + @url + '</a>'); --replace url w/ full href
SET @result = @result + @first; --add updated string to result
--set up for next run
SET @str = @last; --remove corrected section from string
SET @nextMatch = patindex('%www.%', @str); --find start of next 'www.'
END --end while
IF @str IS NOT NULL --add any remaining text back into result
BEGIN
SET @result = @result + @str;
END
RETURN @result;
END;
我的 SQL Server 2012 的一些数据在数据类型 nvarchar(max)
的列 comments
中。
示例数据:
For more information, please visit www.sample.com
If you desire submit a request please go here: www.request.com
Customer Service.
我需要得到这个:
For more information, please visit <a href="www.sample.com">www.sample.com</a>
If you desire submit a request please go here: <a href="www.request.com">www.request.com</a>
Customer Service."
所有链接都以 www
开头。感谢您的帮助
您可以创建这样的函数,接收原始字符串,然后返回结果 w/ html:
CREATE FUNCTION dbo.ufnUrlHref (
@str nvarchar(max)
)
RETURNS nvarchar(max)
AS
BEGIN
DECLARE @nextMatch int = patindex('%www.%', @str); --find start of next 'www.'
DECLARE @result nvarchar(max) = '';
WHILE (@nextMatch != 0)
BEGIN
DECLARE @matchEnd int = charindex(' ', @str, @nextMatch);
DECLARE @strLen int = len(@str);
DECLARE @first nvarchar(max) = substring(@str, 1, @strLen - (@strLen - @matchEnd)); --get all of string up to first url
DECLARE @last nvarchar(max) = substring(@str, @matchEnd + 1, @strLen - @matchEnd); --get rest of string after first url
DECLARE @url nvarchar(255) = substring(@str, @nextMatch, @matchEnd - @nextMatch); --get url
SET @first = replace(@first, @url, '<a href="' + @url + '">' + @url + '</a>'); --replace url w/ full href
SET @result = @result + @first; --add updated string to result
--set up for next run
SET @str = @last; --remove corrected section from string
SET @nextMatch = patindex('%www.%', @str); --find start of next 'www.'
END --end while
IF @str IS NOT NULL --add any remaining text back into result
BEGIN
SET @result = @result + @str;
END
RETURN @result;
END;