此方案适用于discuz3.X版本,可解决以下问题:
1.大量帖子中有敏感词语需要替换
2.网站升级,帖子里的内链是绝对链接的需要批量更换
3.帖子防采集也可能会用到
打开mysql输入以下代码,执行即可
修改论坛标题
UPDATE pre_forum_thread SET subject=REPLACE(subject,'替换前','替换后');
修改论坛内容:
UPDATE pre_forum_post SET message=REPLACE(message,'替换前','替换后');
修改文章内容
UPDATE pre_portal_article_content SET content=REPLACE(content,替换前','替换后');
拓展
上述只是基础替换,若想替换指定内容时,涉及到正则替换问题,mysql使用正则替换2种思路
思路1:
以下是数据库中的一个表table:
+—-+——————————————+
| id | name |
+—-+——————————————+
| 1 | sdfsf<contact>beijing</contact>sldjfsld |
| 2 | sdfsf<contact>shanghai</contact>sldjfsld |
| 3 | sdfsf<contact>jn</contact>sldjfsld |
| 4 | sdfsf<contact>qd</contact>sldjfsld |
+—-+——————————————+
遇到的要求是:将该表中<contact>到</contact>的内容删除。
众所周知,replace函数是不支持正则表达式的,所以只能采用其他的方法处理。
update tabaleA set name = replace(name, substring(name, locate('<contact>', name),locate('</contact>', name)-locate('<contact>'+10, name)),'');
执行之后,报错:Truncated incorrect DOUBLE value解决办法,经过查询发现是concat(Str,’’)函数错误问题,有的DB支持+操作符,而有的就不可以必须使用concat 函数。
将SQL修改如下:
update t_global_project set name = replace(name, substring(name, locate('<contact>', name),locate('</contact>', name)-locate(concat('<contact>',''), name)),'');
思路2
REGEXP_REPLACE()函数用于模式匹配。它通过匹配字符来替换给定的字符串字符。
此函数为MYSQL8.0版本新增,低于8.0版本没有此函数
UPDATE tableName set `name` = REGEXP_REPLACE(`name`, '<.*>', '') WHERE `name` REGEXP '<.*>';