3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
43.03 ms | 402 KiB | 5 Q