Page 1 of 2
Striping Slashes from form submission
Posted: Tue Sep 03, 2002 4:48 am
by Rebajas
Is there a easy way to strip all the slashes from a form submission? Rather than doing each individual field...
Cheers...
R
Posted: Tue Sep 03, 2002 5:25 am
by twigletmac
Code: Select all
foreach ($_POST as $key => $value) {
$clean_postї$key] = stripslashes($value);
}
Is one way you could do it.
Mac
Posted: Tue Sep 03, 2002 5:35 am
by gite_ashish
Posted: Tue Sep 03, 2002 5:38 am
by twigletmac
The link above won't work properly try:
http://www.php.net/manual/en/function.stripslashes.php
instead and read the user comments.
Mac
Cheers
Posted: Tue Sep 03, 2002 6:32 am
by Rebajas
I'll give that a go.
twigletMac, is there anyway to keep the original variable names?
Cheers,
R
Posted: Tue Sep 03, 2002 6:38 am
by twigletmac
In using the foreach loop you do keep the original variable names the variables are just in a different array $clean_post instead of $_POST.
Use
Code: Select all
echo '<pre>';
print_r($clean_post);
echo '</pre>';
to check the contents of the array.
This way you still have the original contents in $_POST in case you need them.
Mac
Posted: Tue Sep 03, 2002 6:50 am
by phpPete
Have a gander at this article. This pertains to stripping slashes throughout your entire application...
http://www.pinkgoblin.com/quotesarticle.php
Turning that into a function?
Posted: Tue Sep 03, 2002 8:24 am
by Rebajas
Code: Select all
function cleanPost($_POST, $replace) {
foreach ($_POST as $key => $value) {
$cleanPostї$key] = stripslashes($value);
if ($replace == "newLines2Breaks") {
$cleanPostї$key] = ereg_replace("\r\n","<br />",$cleanPostї$key]);
}
}
return ($cleanPostї$key]);
}
I have tried to turn the previous examples into a function so i can use it elsewhere. When i call it though there are no returned results? Any idea what i might be doing wrong?
R
Posted: Tue Sep 03, 2002 8:32 am
by twigletmac
Try:
Code: Select all
function cleanPost($_POST, $replace='') {
foreach ($_POST as $key => $value) {
$cleanPostї$key] = stripslashes($value);
if ($replace == 'newLines2Breaks') {
$cleanPostї$key] = nl2br($cleanPostї$key]);
}
}
return ($cleanPost);
}
Then you call it like so:
Code: Select all
$cleanPost = cleanPost($_POST);
//check contents for debugging
echo '<pre>';
print_r($cleanPost);
echo '</pre>';
Mac
nl2br
Posted: Tue Sep 03, 2002 8:54 am
by Rebajas
I knew about nl2br but always thought it output <br> rather than XHTML style <br />. When did this function change or has it always done it like that?
Cheers - it works beautifully now...
Rebajas
Posted: Tue Sep 03, 2002 8:55 am
by twigletmac
I think they changed it from <br> to <br /> in PHP version 4.1 but I can't say for sure.
Mac
Posted: Tue Sep 03, 2002 9:10 am
by fatalcure
i just use:
Code: Select all
foreach($HTTP_POST_VARS as $k=>$v) {
$$k = stripslashes($v);
}
Preview and edit...
Posted: Tue Sep 03, 2002 9:18 am
by Rebajas
Errr... After all this now I need to know how to reverse the nl2br function

As it was before (The opposite of the nl2br function i was using) puts too many new lines in...
This was supposed to have been a liitle job.
R
Posted: Tue Sep 03, 2002 9:20 am
by twigletmac
To remove the HTML line breaks:
Code: Select all
$without_HTML_line_breaks = str_replace('<br />', '', $with_HTML_line_breaks);
should do the trick.
Mac
...but
Posted: Tue Sep 03, 2002 9:23 am
by Rebajas
keep the original breaks the user put in the first time round?
Sorry, should have said that in the last message
R