搜索+替换mySQL数据


Search + replace mySQL data

我们的论坛是基于phpbb3的,有一段时间我们一直在使用语法高亮模式。我们决定放弃这个模式,回到基本的bbcode标签。长话短说-我需要更新mySQL字段(phpbb_forum_posts, post_text)包含:

Text Text Text Text Text 
[swordfish filename=code.bas]
code code code
123
code code[/swordfish]
Text Text Text Text Text 

Text Text Text Text Text 
[code]
code code code
123
code code[/code]
Text Text Text Text Text 

值得强调的是,[swordfish filename=code.bas]可能包含[swordfish filename=code.bas abc 123]等其他字段。不管在第一个标签是什么,它需要更改为[code]

这有点超出了我的"需要知道"的范围,如果有人可以帮助设计一个mySQL或PHP脚本来实现大规模更新,我将非常感激。

给你:

$regexp = '/'[('/)?swordfish[^']]*']/';
$push = 'REPLACE whatever_table (id, post_text)
          VALUES';
$pull = 'SELECT id, post_text
           FROM whatever_table
          WHERE whatever = condition';
if ( $result = $mysqli->query($pull) ) {
    while ( $row = $result->fetch_assoc() ) {
        $row['post_text'] = preg_replace($regexp, '[$1code]', $row['post_text']);
        $push .= sprintf(
            ' ("%s", "%s"),',
            $mysqli->real_escape_string($row['id']),
            $mysqli->real_escape_string($row['post_text'])
        );
    }
    $result->free();
}
$mysqli->query(rtrim($push, ','));

编辑
请注意,REPLACE声明是不完整的!您必须获取每一列并将其写回来(不仅仅是我提到的两列)。