dongtang2376 2013-11-21 05:35
浏览 578
已采纳

是否需要转义(反斜杠)路径?

It is not uncommon to have to put a path to a file or directory in a string in a program or script’s code. This isn’t usually a problem with paths on *nix systems since they use forward slashes which usually does not need to be escaped in most languages, but Windows uses back slashes for paths which usually do need to be escaped in most languages.

With C(++), you usually do have to escape the slashes of a path in a string because most compilers will emit a warning if you don’t, but I have seen other code, particularly PHP scripts, which do not escape them. For example, I have seen countless examples of configuration files containing lines similar to this:

static $storageDir = 'C:\webgrind\tmp';

Ostensibly you must escape such paths, and I did consider that maybe they were written by people who were not actually using Windows and simply extrapolating configuration data from Linux to Windows without actually testing it, but if that were the case, then the code should fail, yet it seems that it often works.

So do you have to escape paths in strings in code? I’m asking mostly about PHP because that’s where I’ve personally seen this happen the most, but I’m open to other/universal answers as well.

  • 写回答

3条回答 默认 最新

  • donglian3061 2013-11-21 05:42
    关注

    Single quotes inhibit all escape sequences other than \\ and \', so there is no need to escape backslashes within them unless it appears as the final character in the string literal.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!