dkirk Posted September 22, 2007 Share Posted September 22, 2007 I am having a bit of trouble trying to double up on slashes in a file path. What I am trying to do is very similar to the code below: <? $var = "\\wusais\Intranets\Intranets\fpdb\pdf\weinig\00505882.pdf"; $new = preg_replace("\\", "\\\", "$var"); ?> Code above produces the following error: Parse error: parse error, unexpected T_VARIABLE in c:\Inetpub\wwwroot \pages\replace.php on line 12 In the end, $new needs to be: \\\\wusais\\Intranets\\Intranets\\fpdb\ \pdf\\weinig\\00505882.pdf but it seems to be very difficult replacing slashes. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/70225-replace-slashes-with-more-slashes/ Share on other sites More sharing options...
tommyboy123x Posted September 22, 2007 Share Posted September 22, 2007 The \\ are canceling each other out, and then the third \ removes the quotation mark and so PHP reads it as an unexpected variable (no closing quotation mark) on a side note, i really prefer str_replace over preg_replace I think it should be something like $var = "\\wusais\Intranets\Intranets\fpdb\pdf\weinig\00505882.pdf"; $new = str_replace("\\", "\\\\", $var); Basically look at the error your getting. Its not saying the url is bad, just that theres an unexpected variable. I'd recommend getting phpdesigner or something that shows different colors for functions, variables, strings, etc. PHPdesigner is free too :-) Quote Link to comment https://forums.phpfreaks.com/topic/70225-replace-slashes-with-more-slashes/#findComment-352716 Share on other sites More sharing options...
dkirk Posted September 22, 2007 Author Share Posted September 22, 2007 I just tried: <? $var = "\\wusais\Intranets\Intranets\fpdb\pdf\weinig\00505882.pdf"; $new = str_replace("\\", "\\\\", "$var"); ?> and received: \\wusais\\Intranets\\Intranets\\fpdb\\pdf\\weinig05882.pdf almost there? Quote Link to comment https://forums.phpfreaks.com/topic/70225-replace-slashes-with-more-slashes/#findComment-352717 Share on other sites More sharing options...
Fadion Posted September 22, 2007 Share Posted September 22, 2007 <?php $var = "\\wusais\Intranets\Intranets\fpdb\pdf\weinig\00505882.pdf"; $new = str_replace("\\", "\\\\", $var); echo $new; //it prints out: \\wusais\\Intranets\\Intranets\\fpdb\\pdf\\weinig05882.pdf ?> The starting double slashes in the $var string will be considered as one as the first escapes the second, so i u really want four slashes in the beginning of $new u can add them manually: $new = "\\\\" . $new; Also the adress ure using in the $var string is a windows path containing "\", while in a real envoirment ull have paths containing "/". Quote Link to comment https://forums.phpfreaks.com/topic/70225-replace-slashes-with-more-slashes/#findComment-352718 Share on other sites More sharing options...
dkirk Posted September 22, 2007 Author Share Posted September 22, 2007 It will be udes on a company intranet, all file paths are coming from a database and all are windows paths. Adding th four slashes at the begining is no problem but whats going on in the end? weinig05882.pdf Quote Link to comment https://forums.phpfreaks.com/topic/70225-replace-slashes-with-more-slashes/#findComment-352721 Share on other sites More sharing options...
tommyboy123x Posted September 22, 2007 Share Posted September 22, 2007 I can't get the same results your getting... That doesn't seem to make any sense though, because the "|" symbol is nowhere in the code Quote Link to comment https://forums.phpfreaks.com/topic/70225-replace-slashes-with-more-slashes/#findComment-352777 Share on other sites More sharing options...
cooldude832 Posted September 22, 2007 Share Posted September 22, 2007 trying making them into variables Quote Link to comment https://forums.phpfreaks.com/topic/70225-replace-slashes-with-more-slashes/#findComment-352783 Share on other sites More sharing options...
dkirk Posted September 22, 2007 Author Share Posted September 22, 2007 My problem is that the string coming from the database as variable with 2 slashes in the front and 1 slash between each directory. All of the file paths coming from the db are structured this way and there are thousands of records. Once the variable comes from the db, I am passing it on to a javascript. If I pass the variable as is, the script will not work but if I double up on the slashes everything works fine. So, I have to take the file path that is coming out of the db and double up on the slashes. Using only php, everything works fine with the slashes the way they are. Quote Link to comment https://forums.phpfreaks.com/topic/70225-replace-slashes-with-more-slashes/#findComment-352863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.