Replacing backslash with another symbol in PHP

余生颓废 提交于 2019-12-01 21:35:42

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.

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.

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?

That's easy. PHP:

$url = 'http://example.com/?array=' . urlencode(serialize($array)); // html
$array = unserialize($_GET['array']); // server side

Or Javascript:

url = "http://example.com/?array=" + encodeURIComponent(JSON.stringify(array)); // client
$array = json_decode($_GET['array']); // server

(for Javascript you'll have to look up whether encodeURIComponent is correct, and you need the official JSON library as well)

If you're not using a regular expression (which you're not), you should use str_replace instead:

$tmp_name = str_replace('\\', '.-.', $_FILES['...']);

Note that you have to escape the \ with another \ (otherwise it'd escape the following ').

As for the delimiter error - regular expressions need to be enclosed in delimeters, for example /foo/ (/ is the delimiter, foo is the pattern). But, again, there's no need for you to use or worry about regexps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!