Jump to content


Photo

Stripping Slashes Problem


  • Please log in to reply
2 replies to this topic

#1 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 28 June 2006 - 05:52 PM

I am using the stripslashes function but it isn't working correctly here what I got:

$username = $_POST['username'];
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];

$username2 = stripslashes($username);
$Fname2 = stripslashes($Fname);
$Lname2 = stripslashes($Lname);
$email2 = stripslashes($email);
$city2 = stripslashes($city);
$state2 = stripslashes($state);

Say $username = "f\s/d\f3/675"

Now if i echo $username2

It will output f\s/d\f3/675 without stripping anything

Also, how do I strip the forward slashes also?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 June 2006 - 07:06 PM

stripslashes doesnt strip all slashes out of a string, it only strips slashes that is escaping certain characters:

For example you had an apostrophe in your name, like so O'reily if you did this: O\'reily the backslash is escaping the apostrophe. Whn you use stripslashes on that name PHP will remove the slash, it'll also remove the slash if you had two backslashes. Such as \\ would be converted into \

If you want to remove slashes completely use str_replace like so:
<?php

$str = "This //string\\had slashes /\ in\\ it!";

// Only strips out slashes that is escaping characters.
echo stripslashes($str) . " - with stripslashes<br />\n\n";

// use str_replace to remove \ and / characters from our string
// NOTE: two \'s was used below to stop PHP from escaping the quote mark
$str = str_replace(array("\\", "/"), '', $str);

echo $str . '- with str_replace';

?>


#3 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 28 June 2006 - 07:56 PM

Thanks, worked like a charm....I just didn't understand what stripslashes() done exactly I guess




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users