如何从mysql db记录中删除[mytag]某些文本[/ mytag]标签之间的文本?

时间:2011-09-08 05:07:23

标签: mysql

我有一个名为

的表

故事

并且有一列

每个正文包含来自以前CMS的标签之间封装的php / html代码[soltag] [/ soltag]

所以,例如一条记录看起来像这样:

Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting
[soltag]php, $end = ""; if (isset($_GET["vote"])) { $end .= "?vote=" . $_GET["vote"]; if (isset($_GET["vid"])) { $end .= "&vid=" . $_GET["vid"]; }; }; $output = file_get_contents("http://example.com/something.html" . $end);[/soltag] And the body text continues here.

我想删除这些标记之间的所有内容,包括数据库中每条记录的这些标记。

所以,在此之后它应该看起来像:

Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting  And the body text continues here.

我有5000条记录,因此手动至少需要2-3天的机器人工作。

有什么想法我怎么能用mysql命令删除它?

提前致谢。

2 个答案:

答案 0 :(得分:4)

为了类似的目的,我正在寻找类似的东西,这非常有用。

只有一件事......对于下一个搜索相同问题的人。

如果你想要删除结束标记,你应该在SUBSTRING的第三个参数中对结束标记的字符串长度求和:

  

更新故事SET [body] = REPLACE([body],

                 SUBSTRING([body], 
                           INSTR([body],'[soltag]'), 
                           INSTR([body],'[/soltag]') +CHAR_LENGTH('[/soltag]') -INSTR([body],'[soltag]')
                          )
                 ,'');

希望有人帮助! :)

答案 1 :(得分:1)

使用SUBSTRING()INSTR()来帮助完成此任务。

在更新列之前,请确保您拥有正确的数据。

SELECT REPLACE([body],
                     SUBSTRING([body], 
                               INSTR([body],'[soltag]'), 
                               INSTR([body],'[/soltag]') -INSTR([body],'[soltag]')
                              )
                     ,'') AS NewBody
From [stories]

如果INSTR()被一个人关闭,请根据需要进行调整。

然后您可以更新此表中的所有列。

UPDATE stories
SET [body] = REPLACE([body],
                     SUBSTRING([body], 
                               INSTR([body],'[soltag]'), 
                               INSTR([body],'[/soltag]') -INSTR([body],'[soltag]')
                              )
                     ,'')