用PHP中的另一个符号替换反斜杠 [英] Replacing backslash with another symbol in PHP

查看:406
本文介绍了用PHP中的另一个符号替换反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在努力用另一个符号(例如'.-。')替换反斜杠来指示反斜杠的位置,因为我无法通过url发送诸如'C \xampp \etc。'之类的字符串作为GET变量所以我想我会先用另一个符号替换该字符串中的反斜杠,然后通过url发送,然后将它们替换回处理它的PHP文件中的反斜杠。虽然有没有更好的方式通过url发送这样的字符串?因为当我尝试如下脚本时:

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

它变成了php错误,因为\也被用作分隔符。



任何人都可以帮我解决这个问题吗?



感谢您!

顺便说一句,如果我能够通过url发送一个完整的数组,那么这个问题就可以解决,但我认为这是不可能的?

解决方案

preg_replace中使用的正则表达式应包含在一对分隔符中,并尝试使用 \\\\ 而不是 \ 为:

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

编辑:



  $ str = preg_replace('{\.-\。} , \\,$海峡); 

您需要转义以匹配一个字面点。


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?

解决方案

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.

这篇关于用PHP中的另一个符号替换反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆