1

I've got a string containing values such as "hello world\' hello world\'" and I'd like to get rid of the escape characters (the backslashes.)

I've tried the following code:

str_replace("\\", "", $data);

But it doesn't seem to work.

4
  • 1
    What do you want to do in the first place? Commented Jan 14, 2012 at 20:20
  • 6
    str_replace returns the result. Are you assigning it to anything? Commented Jan 14, 2012 at 20:21
  • I bet the fine php manual entry describing the behavior @takteek mentions works. Always consult the PHP documentation before asking why something doesn't work or you'll get A-holes like me yelling at you :) Commented Jan 14, 2012 at 20:23
  • Wow sorry that was a fail on my part wasn't assigning it to a variable duhh! thanks for the help! Commented Jan 14, 2012 at 20:23

3 Answers 3

6

If all you want to do is to get rid of backslashes, then there's a very handy PHP function that accomplishes just that

$var = stripslashes($var);
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you're using $var as the last parameter in str_replace() instead of $data, it should work fine.

$var = "hello world\' hello world\'";
echo $var . "<br />";
echo str_replace("\\", "", $var) . "<br />";

Output:

hello world\' hello world\'
hello world' hello world'

Comments

1

this should work great for you you were not referencing the variable $var correctly in php replace subject parameter also assuming you need to replace the \' you were putting \ which searches for it hence nothing was found to be replaced hope this helps

$var = "hello world\' hello world\'";

echo str_replace("\'","",$var);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.