tags:

views:

31

answers:

3

Hello,

I have the local file path as c:\new folder\pdf\today\k.pdf I want to replace the c:\ with file:\\c|

I tried str_replace('','',) but I get error due to the slash, no stripslash will not work.

Thanks Jean

+1  A: 

Try:

$string = 'c:\new folder\pdf\hello.pdf';
$new_str = str_replace("c:\\", "file:\\\\\\c|", $string);
echo $new_str;

Result:

file:\\\c|new folder\pdf\hello.pdf
Sarfraz
I want to replace the c:\ with file:\\\c|
Jean
@Jean, i have updated my answer, please check
Sarfraz
@sarfrazMy current path is c:\new folder\pdf\hello.pdfI want to change it to file:\\\c|I used striplashes to remove all "\\"
Jean
@sarfrazmore over the "\" in $string = 'c:\'; will flag an error as shown above its all red after the 'c:\';
Jean
I just tried my current code, it works, check plz
Sarfraz
A: 

You can do this:

<?php
$a = 'c:\new folder\pdf\today\k.pdf';

$a = str_replace('c:\\','file:\\\\\\c|',$a);

var_dump($a); // print string(36) "file:\\\c|new folder\pdf\today\k.pdf"

?>
codaddict
+2  A: 

Isn't c:\new folder\pdf\today\k.pdf to file:///c|new folder/pdf/today/k.pdf?

If so, following will work, without regex

$x='c:\new folder\pdf\today\k.pdf';

$x='file:///'.str_replace('\\','/',str_replace(':\\','|',$x));

file:///c|new folder/pdf/today/k.pdf will return

S.Mark
This is the usual format for `file:` URLs. file-double-backslash doesn't make any sense.
bobince