Need help with trimming whitespace from $_POST data

I am trying to write a function that trims excess whitespace from all data in the $_POST array, including excess spaces from within the data using preg_replace. The regex is not a big problem, but the function is! Here’s a sample, taken from a book, that I would like to be able to use/adapt/improve:

function clean($string) {
$string = preg_replace('/\\s+/', ' ', trim($string));
$string = stripslashes(strip_tags($string));
return $string; }

Obviously the author wrote that with direct access to a single string of text in mind. How can I adapt that kind of function so that it iterates through the contents of the $_POST array and modifies it as necessary?

I understand that I can’t use “foreach” to get the data from the array because it only acts on a copy: is that right? If so, what is the best alternative?

Many thanks.

Sure you can:

foreach ($_POST as $key => $value) {
  $_POST[$key] = clean($value);

Or more succintly:

$_POST = array_map('clean', $_POST);

Thanks for replying, Dan.

I think I get it: set up the function and then call it with array_map.

In the example function that I posted, what should I replace $string with?