Log in

View Full Version : repace '/' to '\'?



writeman
03-22-2007, 07:07 AM
hi all
My filepath looks like w:/aaa/bbb/ccc/abc.doc.
I want to replace all '/' to '\'.
my code don't work and occurs error

for($i=0; $i<count($VFILE); $i++){
$tmp= $VFILE[$i];
$tmpstr='';
for($j=0; $j<strlen($tmp); $j++){
if($tmp[$j]== '/'){$tmp[$j]= "\";}
$tmpstr=$tmpstr . $tmp[$j];
}
$VFILE[$i]= $tmpstr;
}

if I change the red section code to if($tmp[$j]== '/'){$tmp[$j]= "\\";}
It works, but I got two '\\'?
Are there some codes to fit my program?

keren2
03-22-2007, 07:20 AM
<?
$url = "/aaa/bbb/ccc/abc.doc";

$newurl = str_replace("/", "\\", $url);

echo $newurl;
?>

basically, if u use an "\" then it will escape the next character, so using "\\" will excape the second "\" so it will be readable by the php engine as "\" not
"\\" anymore :D

about the $url, maybe u can use $_SERVER["DOCUMENT_ROOT"]; or just declare it by yourself.