php stripslashes leaves one slash

Pengume picture Pengume · Jan 13, 2012 · Viewed 8.8k times · Source

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]);

Answer

Eve Freeman picture Eve Freeman · Jan 13, 2012

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']);