3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $keys = array('SELECT','FROM','WHERE','LEFT','ORDER'); $sql = "Select id, nome FROM table as t WHERE t.id = 123 LEFT JOIN t2 ON t2.id = t.id ORDER BY t.name "; $str = preg_replace('!\s+!', ' ', $sql); $arr = explode(' ', $str); $j = -1; $out = []; for ($i = 0; $i < count($arr); $i++) { if (in_array(strtoupper($arr[$i]), $keys)) { if(($kw = array_search(strtoupper($arr[$i]), $keys)) !== false) { unset($keys[$kw]); } $j++; $out[$j][0] = $arr[$i]; $out[$j][1] = ''; continue; } if ($j > -1) { $out[$j][1] = $out[$j][1] . $arr[$i] . ' '; } } var_dump($out);
based on qMKs3
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.3.0rc3
array(5) { [0]=> array(2) { [0]=> string(6) "Select" [1]=> string(9) "id, nome " } [1]=> array(2) { [0]=> string(4) "FROM" [1]=> string(11) "table as t " } [2]=> array(2) { [0]=> string(5) "WHERE" [1]=> string(11) "t.id = 123 " } [3]=> array(2) { [0]=> string(4) "LEFT" [1]=> string(24) "JOIN t2 ON t2.id = t.id " } [4]=> array(2) { [0]=> string(5) "ORDER" [1]=> string(11) "BY t.name " } }