Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace "\" using str_replace() in PHP?

Tags:

php

I would like to remove all back slashes from strings on my site. I do not wish to use strip_slashes(), because I want to keep forward slashes.

This is the code I am trying:

echo str_replace("\", "", "it\'s Tuesday!");

I want to find the backslash in any given string and remove it. But, this code is not working right.

Error:

syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

What could I be doing wrong?

like image 676
user1759682 Avatar asked Oct 30 '12 14:10

user1759682


2 Answers

The backslash is actually escaping the closing quote in your string.

Try echo str_replace("\\","","it\'s Tuesday!");

like image 72
sevenseacat Avatar answered Sep 25 '22 13:09

sevenseacat


Try and get the result:

$str = "it\'s Tuesday!";

$remove_slash = stripslashes($str);

print_r($remove_slash);

Output: it's Tuesday!

like image 31
Er.Sangeeta Avatar answered Sep 25 '22 13:09

Er.Sangeeta