After a short discussion on the subject here (at Boxen, our computer club), we have decided that this is one of those places where a regular expression would be substantially slower than a simple function as the one bellow. Also, thanks to dennismp on www.experten.dk for help with this function. If you wish to see it in action, my portfolio is based on it ( my portfolio )
while (true) {
$begpos = strpos($node_data, "<p >//</p >"); //where does the first table begin
if ($begpos === false) break; //all tables have been created, no need to check more
$endpos = strpos($node_data, "<p >//</p >", $begpos + 11); //table end tag
$lines = explode("\n", substr($node_data, $begpos, $endpos));
$inTable = false;
$firstRow = true;
foreach($lines as $line) {
switch($line) {
case '<p >//</p >':
if( $inTable ) {
$table_data .= " </tr>\n</table>";
} else {
$table_data = "<table class=\"wikitable\">\n";
}
$inTable = $inTable ? false : true;
break;
case '<p >/</p >':
if( $firstRow ) {
$firstRow = false;
$table_data .= " <tr>\n";
} else {
$table_data .= " </tr>\n <tr>\n";
}
break;
default:
if ($inTable) $table_data .= " <td class=\"wikitable\"><div>" . substr($line, 4, -5) . "</div></td>\n";
}
}
$temp = substr($node_data, 0, $begpos);
$temp .= $table_data;
$temp .= substr($node_data, $endpos + 11);
$node_data = $temp;
}