Log in

View Full Version : Strip bbcode from posts with mysql query


Scathefire
07-06-2011, 12:44 PM
Hello,

I am wanting to remove formatting from post text such as font, size, and color.

sample text

I need to preserve things such as bold/italic/underline. So the above text would turn out like:

sample text


Is there a database query I could run on the posts table that would accomplish this? Can someone provide an example?

kh99
07-06-2011, 02:18 PM
I don't know if there's an easy way to do it using a query, but you can use a small php script. Borrowing a nice script that Adrian posted in this thread https://vborg.vbsupport.ru/showthread.php?t=266166 , you could do something like this:


<?php

error_reporting(E_ALL ^ E_NOTICE ^ 8192);

require('./global.php');

$result = $vbulletin->db->query_read("SELECT postid, pagetext FROM " . TABLE_PREFIX . "post");

while ($post = $vbulletin->db->fetch_array($result)) {
$new = preg_replace("#\[/?(color|size|font)(=\s*\"?.*?\"?\s*)?]#i", '', $post['pagetext']);
if ($new != $post['pagetext'])
{
$vbulletin->db->query_write(fetch_query_sql(
array('pagetext' => $new),
'post',
'WHERE postid = ' . $post['postid']
));
}
}

die('Done');



(I tried this out, but you'd probably want to back up the db before running this, just in case).