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.