Search code examples
phpseparator

Replacing a specific character in php


I have a variable having a path to a file which is like the following:

$path = "\home\ashutosh\Desktop\imgdone\0001.jpeg"

I want that \ be replaced by this / in order for me to read that path. I have used str_replace() function to replace but the PHP code is not able to recognize that.

str_replace( "\", "/", $path );

Please help


Solution

  • you need to escape the \ like that : str_replace('\\', '/', $path);

    I explain a bit : "\" tell php to not end at the quote that follow \.

    you use it in this case : "hello I'm \"The Cat\", the super hero". so the \ is a special char, used to escape the one that follow. if you want to use it, you need to escape it with himself : \\