sql - 如果与 SQL 中给定的匹配 URL 匹配,如何从正文中删除锚标记
问题描述
我有一个sample input body
下面,其中包含anchor tag
-
<p>I personally use a <a href="http://www.prevention.com/fitness/6-must-have-mobility-tools">massage stick</a> on a regular basis, and highly recommend one called the Tiger Tail, which retails for less than $30. I keep it by my bedside to address the exercise-related leg cramps I occasionally experience during the night. Since the cramps I generally get attack my smaller muscles, making them difficult to treat with regular stretching, I use the Tiger Tail instead. After a few minutes of rolling over the cramping muscle, the pain typically disappears or at least has been significantly reduced.</p>
现在,如果下面给出的URL
包含在上面body
,那么whole anchor tag
应该是removed
(excluding wrapped text)
-
http://www.prevention.com/fitness/6-must-have-mobility-tools
身体的预期输出-
<p>I personally use a massage stick on a regular basis, and highly recommend one called the Tiger Tail, which retails for less than $30. I keep it by my bedside to address the exercise-related leg cramps I occasionally experience during the night. Since the cramps I generally get attack my smaller muscles, making them difficult to treat with regular stretching, I use the Tiger Tail instead. After a few minutes of rolling over the cramping muscle, the pain typically disappears or at least has been significantly reduced.</p>
注意 -解决方案应该generic and dynamic
是no static data
.
提前致谢!
解决方案
I created this SQL function for the purpose described:
create or alter function [dbo].[TMFN_RmAnchorLinksNotText](@HTMLText nvarchar(max))
returns nvarchar(max)
as
begin
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX('<a',@HTMLText)
SET @End =CHARINDEX('>',@HTMLText,CHARINDEX('<a',@HTMLText))
SET @Length = (@End - @Start) + 1
-- Remove all anchor start-tags, inc. href='xxx'.
WHILE @Start > 0 AND @End > 0 AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')
SET @Start = CHARINDEX('<a',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<a',@HTMLText))
SET @Length = (@End - @Start) + 1
END
-- Remove all anchor end-tags:
SET @HTMLText = REPLACE(LTRIM(RTRIM(@HTMLText)),'</a>','')
return @HTMLText
end
推荐阅读
- exception - 从使用 Visual C++ 2019 编译的应用程序加载时,第 3 方 DLL 中的访问冲突
- java - 如何使用 DCEVM/Hotswap 代理进行 jar 文件更新?
- angular - Strapi API 的 Angular 表单值
- javascript - React Native 警告:在现有状态转换期间无法更新(例如在“渲染”中)
- android - 卸载应用程序后,无法再在我的设备上重新安装我的 Android 应用程序。(错误类型 3:MainActivity 不存在)
- java - 墙壁内的Java迷宫并获得所有可能的路径
- c# - Unity CSharp 2D 纹理
- c++ - 哪些主机与 Winsock 和 C++ 兼容
- laravel - Laravel Spark 9.0 未定义的货币标签
- google-apps-script - Mailsender 在手动运行时运行良好,而在时间触发时使用非实际数据