3v4l.org

run code in 300+ PHP versions simultaneously
<?php <?php //> Will count number of <[a-z]> tag and </[a-z]> tag (will also validate the order) //> Note br should be in the form of <br /> for not causing problems function validHTML($html,$checkOrder=true) { preg_match_all( '#<([a-z]+)>#i' , $html, $start, PREG_OFFSET_CAPTURE ); preg_match_all( '#<\/([a-z]+)>#i' , $html, $end, PREG_OFFSET_CAPTURE ); $start = $start[1]; $end = $end[1]; if (count($start) != count($end) ) throw new Exception('Check numbers of tags'); if ($checkOrder) { $is = 0; foreach($end as $v){ if ($v[0] != $start[$is][0] || $v[1] < $start[$is][1] ) throw new Exception('End tag ['.$v[0].'] not opened'); $is++; } } return true; } //> Usage:: try { validHTML('<p>hello</p><li></li></p><p>'); } catch (Exception $e) { echo $e->getMessage(); }
Output for 7.0.0 - 7.0.1
Parse error: syntax error, unexpected '<', expecting end of file in /in/FsUfB on line 3
Process exited with code 255.
Output for 5.5.24 - 5.5.30, 5.6.8 - 5.6.16
Parse error: syntax error, unexpected '<' in /in/FsUfB on line 3
Process exited with code 255.

preferences:
166.43 ms | 1387 KiB | 25 Q