3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $depth = 0; $traverse = [0, 0, 0]; $tags = ['table', 'row', 'cell']; $regex = []; foreach ($tags as $key=>$tag) $regex[$key] = '/(\['.$tag.'((?:[\s\.\#]+[a-z]*)*)\]+[^\[]((?:.|\s)*?(?=(?:\['.$tag.')|$)))/'; echo var_export($tags),PHP_EOL; echo var_export($regex),PHP_EOL; $content = "[table] [row] [cell] [cell] [row] [cell] [cell] [table] [row] [row] [cell] [row] "; $f_parse = function($matches) use (&$depth, &$traverse, $tags, $regex, &$f_parse) { $traverse[++$depth]++; if ($depth < count($regex)) { preg_replace_callback( $regex[$depth], $f_parse, $matches[0] ); } /* echo "$depth | ${traverse[$depth]} | $tags, ${regex[$depth]}", PHP_EOL; $result = ""; if ($depth < count($regex)) { preg_replace_callback( $regex[$depth], $f_parse, $matches[0] ); } return $result;*/ echo $depth, ' | ', $traverse[$depth], ' | ', PHP_EOL; $traverse[$depth--]--; return $depth; }; $content = preg_replace_callback( $regex[$depth], $f_parse, $content ); echo $content, "\n"; ?>
based on oJ0HA
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.3.0beta1
array ( 0 => 'table', 1 => 'row', 2 => 'cell', ) array ( 0 => '/(\\[table((?:[\\s\\.\\#]+[a-z]*)*)\\]+[^\\[]((?:.|\\s)*?(?=(?:\\[table)|$)))/', 1 => '/(\\[row((?:[\\s\\.\\#]+[a-z]*)*)\\]+[^\\[]((?:.|\\s)*?(?=(?:\\[row)|$)))/', 2 => '/(\\[cell((?:[\\s\\.\\#]+[a-z]*)*)\\]+[^\\[]((?:.|\\s)*?(?=(?:\\[cell)|$)))/', ) Notice: Undefined offset: 3 in /in/KnMrE on line 27 3 | 1 | 3 | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 1 | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 1 | 1 | 00
Output for hhvm-3.15.4
array ( 0 => 'table', 1 => 'row', 2 => 'cell', )1 array ( 0 => '/(\\[table((?:[\\s\\.\\#]+[a-z]*)*)\\]+[^\\[]((?:.|\\s)*?(?=(?:\\[table)|$)))/', 1 => '/(\\[row((?:[\\s\\.\\#]+[a-z]*)*)\\]+[^\\[]((?:.|\\s)*?(?=(?:\\[row)|$)))/', 2 => '/(\\[cell((?:[\\s\\.\\#]+[a-z]*)*)\\]+[^\\[]((?:.|\\s)*?(?=(?:\\[cell)|$)))/', )1 3 | 1 | 3 | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 1 | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 1 | 1 | 00