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条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?