PDA

View Full Version : PHP help with str_replace



sdasevne
01-02-2008, 05:38 PM
In my email form submit program, I added a statement like that shown below to change any backslashes (\) to another character, to prevent the injection of newline or return characters that might make it possible for spammers to enter bcc: or other unwanted mail parameters.

When I add this line, I get an Undefined Variable error message. Can someone tell me what I have to do to scan each field and replace all slashes?

foreach ($_POST as $Field=>$Value)
{
// $Value = str_replace("\","?",$Value); <--- causes an error message
$MsgBody .= "$Field: $Value\n";
}

linFox
01-02-2008, 05:42 PM
Ironically enough, the escape character (the backslash) that you're trying to remove is escaping the closing doublequote on the first parameter.
Just escape it: str_replace("\\","?",$Value);

sdasevne
01-02-2008, 06:01 PM
str_replace("\\","?",$Value);

Thanks. That worked well enough. It actually replaced all single slashes with TWO question marks, but that's good enough! At least it got rid of the slashes...