How do you check all items in $_POST?

Is there a way of checking all of the items in $_POST in one line, instead of each item individually? It seems stupid to repeat one regular expression numerous times if you want it to apply to all items. e.g.

if (preg_match('/foo/', $_POST['name']))

would be much better as

if (preg_match('/foo/', $_POST['everything']))

I guess that it might involve “foreach”, but I don’t understand how that works or how to use it. I tried Googling for this but not knowing what to search for made that very difficult…

$_POST is just an array, so looping would work…

foreach ($_POST as $postKey => $postVal)
if (preg_match('/foo/', $_POST[$postKey]))
// whatevz.

However everything in POST can even include things like your submit button, so specific rules may not apply to ALL of POST.

foreach($_POST as $key => $value){
  if(preg_match('/foo/', $value)){

Any clearer? :slight_smile:

Thanks to both of you.

I think that’s exactly what I needed.