I’m trying to use a foreach loop to search for word in $_POST, but it doesn’t work? Help is preciated.
$unsafeWords = array('content-typ','bcc','cc');
foreach ($_POST as $key => $input) {
foreach ($unsafeWords as $value) {
$_POST = str_ireplace($value, "", $input) ;
}
}
Don’t overwrite the $_POST array with a string
though I dislike overwriting the original $_POST array and would prefer to build a new array of cleaned values
Note that you don’t need to loop the $unsafeWords array, but can pass an it as an array directly to str_ireplace()
EDIT
Example of using the $unsafeWords array as an argument for str_ireplace() rather than looping through it with foreach() and calling str_ireplace() for each entry.
and you’re not replacing with a space, you’re replacing with an empty string (effectively removing the unsafe strings from your $_POST vars)
EDIT 2
Not quite… if you’re just adding it as an extra line within the loop, you’ll overwrite your previous substitutions.
Do it as: