Replace single slash with double slash, php

how to replace a single slash with a double slash? I have this text:

"/data/folder/"

 and i need get

"//data//folder//"

I am trying to replace, but getting an error:

$data = str_replace("\", "\\", $data);
+3
source share
4 answers

You want to replace the slash, but yours str_replacehas a backslash.

Try:

$data = str_replace("/", "//", $data);

The reason for the error:

\shielding is used. Thus, \in "\"fact eludes the second ".

+6
source

Are you talking about backslashes or normal backslashes? 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));
+4
source

, , escape- , ". :

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

from what you say, you most likely want to use slashes rather than backslashes, as shown in @codaddict figure.

+3
source

You can use the following code:

<?php  $str = addcslashes("Hello World!","W");?>  
<?php echo $str;?> <br/>    
<?php $str2 = addcslashes($str,"W");     
echo $str2;        
?>
-1
source

All Articles