how replace single slash with double slash? I have this text:
"/data/folder/"
and i need get
"//data//folder//"
i try with replace, but get error:
$data = str_replace("\", "\\", $data);
You're talking about backslashes or normal slashes ? anyway check the code below for both of them :
$str = '\dada\dsadsa';
var_dump(str_replace('\\', '\\\\', $str));
$str = '/dada/dadda';
var_dump(str_replace('/', '//', $str));
You want to replace forward slash but your str_replace
is having back slash.
Try:
$data = str_replace("/", "//", $data);
Cause of error:
\
is used escaping. So the \
in "\"
is actually escaping the second "
.
Regarding why you are getting an error, backslashes are escape characters in strings wrapped in double quotes "
. You need to escape them as well:
str_replace("\\", "\\\\", $data);
from what you are saying, you however probably want to use slashes, not backslashes, as shown by @codaddict.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With