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)) { $j++; $out[$j][0] = $arr[$i]; $out[$j][1] = ''; continue; } if ($j > -1) { $out[$j][1] = $out[$j][1] . $arr[$i] . ' '; } } var_dump($out);
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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 " } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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 " } }

preferences:
196.66 ms | 403 KiB | 291 Q