3v4l.org

run code in 300+ PHP versions simultaneously
<?php $bbcode = <<<BBCODE [b]Check out this demo[/b] ¯\_(ツ)_/¯ [table] [**]header1[||]header2[||]header3[||]...[/**] [*]child1.1[|]child1.2[|]child1.3[|]... [*]child2.1[|]child2.2[|]child2.3[|]... [*]child3.1[|]child3.2[|]child3.3[|]... [*]...[|]...[|]...[|]... [/table] simple text [table] [**]a 1[||]and a 2[/**] [*]A[|]B [*]C[|]D [/table] [s]3, you're out[/s] blah BBCODE; function BBTableToHTML($m) { return "<table class=\"ui compact stripet yellow table\">\n" . preg_replace_callback_array( [ '~\[\*\*]([^[]*(?:\[(?!/?\*\*])[^[]*)*)\[/\*\*]~' => 'BBTHeadToHTML', '~(?:\[\*].*\R*)+~' => 'BBTBodyToHTML', '~\[\*](.*)~' => 'BBTBodyRowToHTML' ], $m[1] ) . "</table>"; } function BBTHeadToHTML($m) { return "\t<thead>\n" . "\t\t<tr>\n\t\t\t<th>" . str_replace('[||]', "</th>\n\t\t\t<th>", $m[1]) . "</th>\n\t\t</tr>\n" . "\t</thead>"; } function BBTBodyToHTML($m) { return "\t<tbody>\n{$m[0]}\t</tbody>\n"; } function BBTBodyRowToHTML($m) { return "\t\t<tr>\n\t\t\t<td>" . str_replace('[|]', "</td>\n\t\t\t<td>", $m[1]) . "</td>\n\t\t</tr>"; } echo preg_replace_callback( '~\[table]\R*([^[]*(?:\[(?!/?table])[^[]*)*)\R*\[/table]~', 'BBTableToHTML', $bbcode );
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
[b]Check out this demo[/b] ¯\_(ツ)_/¯ <table class="ui compact stripet yellow table"> <thead> <tr> <th>header1</th> <th>header2</th> <th>header3</th> <th>...</th> </tr> </thead> <tbody> <tr> <td>child1.1</td> <td>child1.2</td> <td>child1.3</td> <td>...</td> </tr> <tr> <td>child2.1</td> <td>child2.2</td> <td>child2.3</td> <td>...</td> </tr> <tr> <td>child3.1</td> <td>child3.2</td> <td>child3.3</td> <td>...</td> </tr> <tr> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> </tbody> </table> simple text <table class="ui compact stripet yellow table"> <thead> <tr> <th>a 1</th> <th>and a 2</th> </tr> </thead> <tbody> <tr> <td>A</td> <td>B</td> </tr> <tr> <td>C</td> <td>D</td> </tr> </tbody> </table> [s]3, you're out[/s] blah

preferences:
155.11 ms | 410 KiB | 5 Q