Reply to Re: replace single slash with double slash

Your name:

Reply:


Posted by Steve on 09/22/07 04:09

"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
news:qfydnT5dxKbCH2nbnZ2dnUVZ_v6rnZ2d@comcast.com...
> dkirkdrei@yahoo.com wrote:
>> On Sep 21, 9:32 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>>> dkirkd...@yahoo.com wrote:
>>>> 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
>>> Why go to all that trouble? str_replace is much easier and faster.
>>>
>>> --
>>> ==================
>>> Remove the "x" from my email address
>>> Jerry Stuckle
>>> JDS Computer Training Corp.
>>> jstuck...@attglobal.net
>>> ==================- Hide quoted text -
>>>
>>> - Show quoted text -
>>
>> tried:
>>
>> <?
>> $var = "\\wusais\Intranets\Intranets\fpdb\pdf\weinig\00505882.pdf";
>> $new = str_replace("\\", "\\\\", "$var");
>> ?>
>>
>> and received:
>>
>> \\wusais\\Intranets\\Intranets\\fpdb\\pdf\\weinig05882.pdf
>>
>
> First of all,
>
> $var = "\\wusais\Intranets\Intranets\fpdb\pdf\weinig\00505882.pdf";
>
> isn't really valid. The backslash character is the escape character. In
> this case all but the last one have a non-escapable character after them,
> so PHP just puts out the slash. However, if you had something like:
>
> $var = "\\wusais\nbad\tworse";
>
> The \n would be taken as a newline character and \t as a tab character.
> And in the case of your last backslash, the \00505882 is taken as a
> character with numberic value '005' (in octal), followed by 05882.
>
> To get good output, you need to start with good input. Then something
> like:
>
> Where is your string coming from? Are you actually defining it like that?
> If so, it's invalid.

i'm not sure what an invalid string looks like, however you are explaining
his results accurately.

depending on the environment, which is (almost) safely assumed as windows,
php does it's own converting of the slashes. so, a unc like this
\\server\share\resource is interpreted exactly the same as
//server/share/resource...on windows. here's an easy fix...


$var = '\\server\share\resource';

no need to do a replace and no need to worry about escape characters. in
php, what is inside quotes is parsed where an indication is given that
parsing should be done. jerry describes some of them above. tics (single
quotes) let php know that no parsing of the string should be performed.

if you have control over the creating of that path, just use forward-slashes
and all will be well, and os independent. if you don't, then php will
already treat the string properly (as in, you retrieve the path from a
database query). i don't need to mention that if you have control of the
string and are using double quotes and backslashes, what those results will
get you. jerry explained that already.

cheers.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация