Striping Slashes from form submission

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

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

Post by Rebajas »

Is there a easy way to strip all the slashes from a form submission? Rather than doing each individual field...

Cheers...

R
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Code: Select all

foreach ($_POST as $key => $value) {
    $clean_postї$key] = stripslashes($value);
}
Is one way you could do it.

Mac
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Rebajas
Forum Newbie
Posts: 16
Joined: Tue Aug 20, 2002 9:35 am
Location: http://www.rebajas.co.uk/

Cheers

Post by Rebajas »

I'll give that a go.

twigletMac, is there anyway to keep the original variable names?

Cheers,

R
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Have a gander at this article. This pertains to stripping slashes throughout your entire application...

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?

Post by Rebajas »

Code: Select all

function cleanPost($_POST, $replace) {
	foreach ($_POST as $key => $value) { 
		$cleanPostї$key] = stripslashes($value);
		if ($replace == "newLines2Breaks") {
			$cleanPost&#1111;$key] = ereg_replace("\r\n","<br />",$cleanPost&#1111;$key]);
		&#125;
	&#125;
	return ($cleanPost&#1111;$key]);
&#125;
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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try:

Code: Select all

function cleanPost($_POST, $replace='') { 
   foreach ($_POST as $key =&gt; $value) { 
      $cleanPost&#1111;$key] = stripslashes($value); 
      if ($replace == 'newLines2Breaks') { 
         $cleanPost&#1111;$key] = nl2br($cleanPost&#1111;$key]); 
      } 
   } 
   return ($cleanPost); 
}
Then you call it like so:

Code: Select all

$cleanPost = cleanPost($_POST);
//check contents for debugging
echo '&lt;pre&gt;'; 
print_r($cleanPost); 
echo '&lt;/pre&gt;';
Mac
Rebajas
Forum Newbie
Posts: 16
Joined: Tue Aug 20, 2002 9:35 am
Location: http://www.rebajas.co.uk/

nl2br

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I think they changed it from <br> to <br /> in PHP version 4.1 but I can't say for sure.

Mac
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

i just use:

Code: Select all

foreach($HTTP_POST_VARS as $k=&gt;$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...

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To remove the HTML line breaks:

Code: Select all

$without_HTML_line_breaks = str_replace('&lt;br /&gt;', '', $with_HTML_line_breaks);
should do the trick.

Mac
Rebajas
Forum Newbie
Posts: 16
Joined: Tue Aug 20, 2002 9:35 am
Location: http://www.rebajas.co.uk/

...but

Post by Rebajas »

keep the original breaks the user put in the first time round?

Sorry, should have said that in the last message :(

R
Post Reply