我有一个名为description数据库表列,在这个列中有大约1000行。每一行可以包含多个链接。例如:
Some description text
<a href="example11.com">Example 11</a>
description text is continue
<a href="example22.com">Example 22</a>
more description text
我可以更新所有的链接和添加目标空白。在此示例中,文本应更改如下:
Some description text
<a href="example11.com" target="_blank">Example 11</a>
description text is continue
<a href="example22.com" target="_blank">Example 22</a>
more description text
摘要是想要更改链接并添加目标空白。
<a href="example11.com">Example 11</a>
至
<a href="example11.com" target="_blank">Example 11</a>
可以使用replace
函数将一段文本替换为另一段文本。
例如,下面将的所有实例替换为
REPLACE(myField, '<a href', '<a target="_blank" href');
在您的问题中提供的文本上使用上述内容将输出以下内容:
Some description text
<a target="_blank" href="example11.com">Example 11</a>
description text is continue
<a target="_blank" href="example22.com">Example 22</a>
more description text
当然,target=“_blank”
不必遵循href
,因此这是完全有效的HTML。
然而,这并不是一个完美的解决方案。如果锚点标记的布局与不完全相同(即,在
a
后面有一个双空格或其他东西,而不是href
),则这将不起作用。
要更新数据,只需使用replace
使用简单的update
语句:
UPDATE MyTable
SET myField = REPLACE(myField, '<a href', '<a target="_blank" href');
工作小提琴显示了一个简单的情况。