3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $pattern = "/(SELECT|FROM|WHERE|LEFT JOIN|ORDER BY) (.+?)(?= SELECT|FROM|WHERE|LEFT JOIN|ORDER BY|$)/m"; $sql = "SELECT id, nome FROM table as t WHERE t.id = 123 LEFT JOIN t2 ON t2.id = t.id ORDER BY t.name "; preg_match_all($pattern, $sql, $arr, PREG_SET_ORDER); var_dump($arr);
based on 3pKFc
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.3.0rc2
array(4) { [0]=> array(3) { [0]=> string(16) "FROM table as t " [1]=> string(4) "FROM" [2]=> string(11) "table as t " } [1]=> array(3) { [0]=> string(17) "WHERE t.id = 123 " [1]=> string(5) "WHERE" [2]=> string(11) "t.id = 123 " } [2]=> array(3) { [0]=> string(29) "LEFT JOIN t2 ON t2.id = t.id " [1]=> string(9) "LEFT JOIN" [2]=> string(19) "t2 ON t2.id = t.id " } [3]=> array(3) { [0]=> string(15) "ORDER BY t.name" [1]=> string(8) "ORDER BY" [2]=> string(6) "t.name" } }