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|$)/im"; $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); var_dump($arr);
based on 9IjQe
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.2.6
array(3) { [0]=> array(5) { [0]=> string(7) "Select " [1]=> string(16) "FROM table as t " [2]=> string(17) "WHERE t.id = 123 " [3]=> string(29) "LEFT JOIN t2 ON t2.id = t.id " [4]=> string(15) "ORDER BY t.name" } [1]=> array(5) { [0]=> string(6) "Select" [1]=> string(4) "FROM" [2]=> string(5) "WHERE" [3]=> string(9) "LEFT JOIN" [4]=> string(8) "ORDER BY" } [2]=> array(5) { [0]=> string(1) " " [1]=> string(12) " table as t " [2]=> string(12) " t.id = 123 " [3]=> string(20) " t2 ON t2.id = t.id " [4]=> string(7) " t.name" } }