Jump to content

replace slashes with more slashes?


dkirk

Recommended Posts

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.

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

<?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 "/".

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.