3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = '<h2>Spiders</h2> <h2>Beetles</h2> <h3>External morphology</h3> <h4>Head</h4> <h4>Thorax</h4> <h4>Legs</h4> <h3>Anatomy and physiology</h3> <h2>Ants</h2>'; preg_match_all('/<h(\d)>([^<]+)/', $html, $matches); $cdepth = $matches[1][0] - 1; foreach ($matches[1] as $key => $depth) { if ($depth > $cdepth) { echo "\n" . str_repeat(' ', $cdepth * 4) . "<ol>\n"; } elseif ($depth < $cdepth) { echo "</li>\n" . str_repeat(' ', $depth * 4) . "</ol>\n" . str_repeat(' ', $depth * 4) . "</li>\n"; } else { echo "</li>\n"; } $cdepth = $depth; echo str_repeat(' ', $cdepth * 4) . "<li><a href=\"{$matches[2][$key]}\">{$matches[2][$key]}</a>"; } while ($cdepth-- >= $matches[1][0]) { echo "</li>\n" . str_repeat(' ', $cdepth * 4) . "</ol>\n"; }
Output for 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.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
<ol> <li><a href="Spiders">Spiders</a></li> <li><a href="Beetles">Beetles</a> <ol> <li><a href="External morphology">External morphology</a> <ol> <li><a href="Head">Head</a></li> <li><a href="Thorax">Thorax</a></li> <li><a href="Legs">Legs</a></li> </ol> </li> <li><a href="Anatomy and physiology">Anatomy and physiology</a></li> </ol> </li> <li><a href="Ants">Ants</a></li> </ol>
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
185.96 ms | 408 KiB | 5 Q