Page 1 of 1

Escape Windows slashes

Posted: Sat Apr 15, 2006 9:36 pm
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 :)

Posted: Sat Apr 15, 2006 9:39 pm
by feyd

Code: Select all

str_replace('\\', '\\\\', $string);

Posted: Sat Apr 15, 2006 9:54 pm
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 :(

Posted: Sat Apr 15, 2006 10:01 pm
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)

Posted: Sat Apr 15, 2006 10:13 pm
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 :)