-1

I tried to remove the special character \ from a string. Below is my code:

$str = "Hello\world. It's a beautiful day.";
$str= (explode("\",$str));
echo $str[0];

However i receive an error in the second lineand the issue is the special character
Any suggestions? Thanks in advance.

Antonis
  • 195
  • 1
  • 3
  • 13

1 Answers1

1

To use the literal \ you first need to escape the backslash itself with another backslash like \.

$str = "Hello\world. It's a beautiful day.";
$str= (explode("\\",$str));
echo $str[0];
Sapna Dorbi
  • 115
  • 2
  • 12
  • 1
    Hi, not really good form to post an answer for a question like this reiterating what was already explained to the OP in the comments, even though this is technically correct, its likely to get down voted for that reason. – Wesley Smith Sep 22 '20 at 14:11
  • Hey, I have not really intended to reiterate the answer someone already answered and here the case is both of us have answered the same question simultaneously. You can refer to the time of posting. – Sapna Dorbi Sep 22 '20 at 14:17
  • I did look at the times, anyway, just a heads up ;) – Wesley Smith Sep 22 '20 at 14:18
  • I didn't really mean it, will take utmost care now onwards – Sapna Dorbi Sep 22 '20 at 14:22