Striping Slashes from form submission
Moderator: General Moderators
-
Rebajas
- Forum Newbie
- Posts: 16
- Joined: Tue Aug 20, 2002 9:35 am
- Location: http://www.rebajas.co.uk/
Striping Slashes from form submission
Is there a easy way to strip all the slashes from a form submission? Rather than doing each individual field...
Cheers...
R
Cheers...
R
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Code: Select all
foreach ($_POST as $key => $value) {
$clean_postї$key] = stripslashes($value);
}Mac
- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
The link above won't work properly try:
http://www.php.net/manual/en/function.stripslashes.php
instead and read the user comments.
Mac
http://www.php.net/manual/en/function.stripslashes.php
instead and read the user comments.
Mac
-
Rebajas
- Forum Newbie
- Posts: 16
- Joined: Tue Aug 20, 2002 9:35 am
- Location: http://www.rebajas.co.uk/
Cheers
I'll give that a go.
twigletMac, is there anyway to keep the original variable names?
Cheers,
R
twigletMac, is there anyway to keep the original variable names?
Cheers,
R
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
to check the contents of the array.
This way you still have the original contents in $_POST in case you need them.
Mac
Use
Code: Select all
echo '<pre>';
print_r($clean_post);
echo '</pre>';This way you still have the original contents in $_POST in case you need them.
Mac
Have a gander at this article. This pertains to stripping slashes throughout your entire application...
http://www.pinkgoblin.com/quotesarticle.php
http://www.pinkgoblin.com/quotesarticle.php
-
Rebajas
- Forum Newbie
- Posts: 16
- Joined: Tue Aug 20, 2002 9:35 am
- Location: http://www.rebajas.co.uk/
Turning that into a function?
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]);
}R
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Try:
Then you call it like so:
Mac
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);
}Code: Select all
$cleanPost = cleanPost($_POST);
//check contents for debugging
echo '<pre>';
print_r($cleanPost);
echo '</pre>';-
Rebajas
- Forum Newbie
- Posts: 16
- Joined: Tue Aug 20, 2002 9:35 am
- Location: http://www.rebajas.co.uk/
nl2br
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
Cheers - it works beautifully now...
Rebajas
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
i just use:
Code: Select all
foreach($HTTP_POST_VARS as $k=>$v) {
$$k = stripslashes($v);
}-
Rebajas
- Forum Newbie
- Posts: 16
- Joined: Tue Aug 20, 2002 9:35 am
- Location: http://www.rebajas.co.uk/
Preview and edit...
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
This was supposed to have been a liitle job.
R
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
To remove the HTML line breaks:
should do the trick.
Mac
Code: Select all
$without_HTML_line_breaks = str_replace('<br />', '', $with_HTML_line_breaks);Mac
-
Rebajas
- Forum Newbie
- Posts: 16
- Joined: Tue Aug 20, 2002 9:35 am
- Location: http://www.rebajas.co.uk/
...but
keep the original breaks the user put in the first time round?
Sorry, should have said that in the last message
R
Sorry, should have said that in the last message
R