Escape Windows slashes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Escape Windows slashes

Post by alex.barylski »

Ok, So Feyd solved one problem of mine, but now another problem has popped up :(

I originally developed everything on a Linux production server so everything works nicely on *nix but now I"m trying to get everything to work on Windows and it's a night mare...

Heres my problem...I have numerous javascript functions which are passed file paths...

On Windows, these file paths look like:

Code: Select all

my_function('C:\somedir\something\index.php?whatever=test');
On *nix thus far anyways, it's the other path delimiter...tis why it works without JScript errors...

The problem is, Javascript parses the strings and chokes because of the '\' inside the path Windows leaves...so a '\' MUST be preceeded by another '\' in either single or double quoted strings in most if not all languages I'm aware of.

The other problem, is it would be ALOT of work to find every single path and call add slashes on it or something similiar.

Thankfully I"m using a template engine and the entire ready to render content is at mt disposal before sending to client side...

So what I'm thinking...is...

Could someone show me how in regex I could escape the '\' inside javascript/html strings ONLY (addslashes on the entire buffer won't work) by preceeding it with another '\'

This should solve my problem :)

Will Windows backslashes as paths cause problems in HTML HREF, etc???

Or likely just Javascript strings eh?

So I just need some regex which looks for

Code: Select all

('|").('|")
The escapes '\' with another '\' correct?

Cheers :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

str_replace('\\', '\\\\', $string);
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Well I thought of that, but it won't work...

Well it does, but it causes other side effects...

Like newlines in alert() boxes loose their effect...

Thats why I thought regex would be a more fine tuned approach...but I suppose that wouldn't work to well either :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well, it'd be best to just buckle down and find all these paths and fix them at the source rather than post processing the string data (and requiring regex engine invocation)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

feyd wrote:well, it'd be best to just buckle down and find all these paths and fix them at the source rather than post processing the string data (and requiring regex engine invocation)
I agree, but it'll have to wait, other things which are more important need attending to :)

Cheers :)
Post Reply