Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing backslash with another symbol in PHP

Been struggling with replacing a backslash by another symbol such as '.-.' just to indicate the position of backslashes as I could not send a string such as 'C\xampp\etc.' through url as GET variable so I thought I'd first replace the backslashes in that string by another symbol, then send through url, and then replace them back to backslashes in the PHP file that handles it. Though would there be a better way to send such strings through url? Because when I try a script such as:

$tmp_name = preg_replace("\", ".-.", $_FILES['uploadfile']['tmp_name']);

It turns out into a php error as \ is also used as delimiter..

Could anyone help me out on this?

Thanks in advance!

Btw, if I'd be able to send a full array through url, this whole problem would be solved, but I don't think it's possible?

like image 451
Skyfe Avatar asked Apr 28 '26 09:04

Skyfe


2 Answers

The regex used in preg_replace should be enclosed in a pair of delimiter and also Try using \\\ instead of \ as:

$tmp_name = preg_replace("{\\\}", ".-.", $_FILES['uploadfile']['tmp_name']);

EDIT:

To reverse the substitution you can do:

$str = preg_replace('{\.-\.}',"\\",$str);

You need to escape the . to match a literal dot.

like image 189
codaddict Avatar answered Apr 29 '26 22:04

codaddict


use urlencode()/urldecode().

echo urlencode('C:\xampp\etc'); // C%3A%5Cxampp%5Cetc

BTW: This sounds like a huge security flaw (sending absolute paths by request)


PS: preg_replace() is for regular expressions. Try str_replace() next time.

like image 28
Philippe Gerber Avatar answered Apr 29 '26 23:04

Philippe Gerber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!