php stripslashes leaves one slash

10.1k views Asked by At

Like the subject says I have some $_POST data that I need to strip all slashes from. However, it leaves behind one and when there are errors on the form it prints the post data back to the user so they do not have to re-enter it. When they submit the page again the number of slashes grows considerably with every submit with errors. The code that I have is straightforward and uses the stripslashes($_POST['first']) and then sends back errors. I have tried to also str_replace to get rid of the last \ but this does not work. Any ideas?

Code EDIT--------

   $first =  stripslashes($_POST[f_name]); 
   $first = str_replace('\\' , '', $_POST[f_name]);
5

There are 5 answers

0
Brent Baisley On

Do you have magic_quotes enabled? stripslashes will only remove \ that were used to escape a character. So if you WANT a \ in your text, you need to escape it by using two: \. In which case two \ will be converted to one \ with stripslashes. Do a print_r($_POST) before you do any processing to see what is actually in the POST.

0
Jeremy Kendall On

Disabling magic quotes should fix your problem.

2
Eve Freeman On

stripslashes() only strips the first consecutive backslash (when they are consecutive), because a backslash is used to escape a backslash.

You should use str_replace("\\", "", $_POST['first']);

update if it's front slashes you're trying to remove, use str_replace("/", "", $_POST['first']);

0
shihab mm On

I think your post array includes something taken from database like isn\'t or Wouldn\'t to update to database. Then you do one thing when you read data to a field use:

stripslashes( data['description_to_textarea'] )

Problem fix.

0
The One and Only ChemistryBlob On

I had a similar problem and used a very crude solution....I just implemented the stripslashes function twice in a row, or in your case:

$first_temp =  stripslashes($_POST[f_name]);
$first = stripslashes($first_temp);  

Crude but it worked for me!