To replace any number of cells (and rows) it gets a bit more complicated.
$data = 'Random data. [[cell1][cell2][cell3]][[cell4][cell5][cell6]] More randomness.';
if (preg_match_all('/\[(\[.*?\])\]/s', $data, $matches)) {
$count = count($matches[0]);
for ($i = 0; $i < $count; $i++) {
$workstr = $matches[1][$i];
$tmp = '';
while ($tmp != $workstr) {
$tmp = $workstr;
$workstr = preg_replace('/\[([^\]]*)\]/s', '<td>\1</td>', $workstr, 1);
}
$data = preg_replace('/\[\[.*?\]\]/s', '<tr>'.$workstr.'</tr>', $data, 1);
}
}
Let me try to explain this...
The preg_match_all is used to separate out one row at a time, where anything starting with "[[" and ending with "]]" is a row. The ".*?" in this says to match anything number of characters in a non-greedy manner. You can read about greediness on this page if you like.
Then for each of these rows, it goes through and changes each cell one at a time into the proper format.
Finally it again matches the single row and replaces it with the new cell content it just worked out.