I have several scripts that follow the same logic as yours. What I do is create a function called cleanup:
function cleanup ($arg) {
foreach ($arg as $key => $value) {
$value = strip_tags ($value);
$value = stripslashes ($value);
$value = htmlspecialchars ($value);
$value = trim ($value);
$arg[$key] = $value;
}
return $arg;
}
clean takes any array as it's argument ($arg) and cleans it up for display and reinsertion into hidden form fields. I just run cleanup() right before I need to output any submitted data:
$POST = cleanup ($POST);
one of the lines in cleanup is htmlspecialchars which turns all double quotes into " so it will display properly.