MyBB Community Forums

Full Version: Replace certain words in posts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to replace all instances of a certain word in all posts.
The word filter only works for new posts, so what's the best way to do this?
Not sure if this is the most efficient way, but here's a quick plugin.

Name: inc/plugins/quickcensor.php

<?php
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$plugins->add_hook("parse_message_start", "quickcensor");

function quickcensor_info()
{
	return array(
		"name"			=> "QuickCensor",
		"description"	=> "Custom for Solidus.'",
		"website"		=> "http://mybb.com",
		"author"		=> "Paul H.",
		"authorsite"	=> "http://mybb.com",
		"version"		=> "1.0",
	);
}

function quickcensor_activate()
{
}

function quickcensor_deactivate()
{
}

function quickcensor($message)
{
	$message = str_replace("word you want censored", "replace with", $message);
	$message = str_replace("another word you want censored", "replace with another", $message);
	return $message;
}
?>
need to do it in phpmyadmin for each keyword you want replaced. this is case-insensitive on 'search' normally

update mybb_posts set message = replace(message, 'search', 'replace') where message like '%search%'

it will likely time out, so you will need to write a script to do it in batches. also, copy the posts table first so you have a backup
This term will be in over 80,000 posts, which of the above is more efficient?
Paul, can I disable and remove your plugin after changing, or will it revert?
It hooks into the parser, so it needs to be there all the time. pavemen's solution is the way to go, I think.
Paul's solution only changes the display and is not a permanent solution. mine is permanent but will likely not run all at once.

you are better off writing a small script to run 500-1000 posts then redirectback to the page for the next group. as long as you browser's max redirect setting will let it run that many times. but this would require tracking the max pid of the current group and passing that to the next iteration so you can do a where clause like where message like '%search%' and pid <= ($lastpid+500).

i did something similar when i converted my old forum to mybb

hope you get all that.
(2011-12-10, 02:42 AM)pavemen Wrote: [ -> ]hope you get all that.

I'm afraid I don't. Is it a complex script?
its not too bad. just takes some thinking of how to optimize it. something like this

include "global.php";
$num_to_process = 500;

$query = $db->simple_select("posts", "pid", "message like '%search%'", array("limit"=>$num_to_process));
$pids = array();
while($result = $db->fetch_array($query))
{
    $pids[] = $result['pid'];
}

if(count($pids))
{
    $query = $db->write_query("UPDATE ".TABLE_PREFIX."posts SET message = REPLACE(message, 'search', 'replace') WHERE pid IN (".implode(",", $pids).")");
    header("Location: ".THE_URL_TO_THIS_FILE);
}
else
{
    echo "Done";
}

change $num_to_process to whatever your server will handle and then change "search" and "replace" to the words you want.