foreach ($_POST as $i => $v) { if (preg_match("/^Q\d+$/",$i)) { // Here we insert value $v to database } }