在 PHP 中,我需要转义反斜杠吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3415683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In PHP do i need to escape backslashes?
提问by Jiew Meng
I think this may be a stupid question, but I am quite confused whether I need to escape backslash in PHP.
我认为这可能是一个愚蠢的问题,但我很困惑是否需要在 PHP 中转义反斜杠。
echo 'Application\Models\User'; prints Application\Models\User
echo 'Application\Models\User'; same output
echo 'Application\Model\'User'; gives Application\Model'User
So it's a escape character, shouldn't I need to escape it (\
) if I want to refer to Application\Models\User
?
所以它是一个转义字符,\
如果我想引用它,我不应该需要转义它 ( )Application\Models\User
吗?
回答by Gumbo
In single quoted stringsonly the escape sequences \\
and \'
are recognized; any other occurrence of \
is interpreted as a plain character.
在单引号字符串中,只有转义序列\\
和\'
被识别;任何其他出现的\
被解释为普通字符。
So since \M
and \U
are no valid escape sequences, they are interpreted as they are.
因此,由于\M
和\U
不是有效的转义序列,因此它们被按原样解释。
回答by Artefacto
In single quoted strings, it's optional to escape the backslash, the only exception is when it's before a single quote or a backslash (because \'
and \\
are escape sequences).
在单引号字符串中,可以选择转义反斜杠,唯一的例外是它在单引号或反斜杠之前(因为\'
和\\
是转义序列)。
This is common when writing regular expressions, because they tend to contain backslashes. It's easier to read preg_replace('/\w\b/', ' ', $str)
than /\\w\\b/
.
这在编写正则表达式时很常见,因为它们往往包含反斜杠。preg_replace('/\w\b/', ' ', $str)
比阅读更容易/\\w\\b/
。
See the manual.
请参阅手册。
回答by Sarfraz
Since your last example contains a quote ('
), you need to escape such strings with addslashes
function or simply adding a slash yourself before it like this:
由于您的上一个示例包含引号 ( '
),因此您需要使用addslashes
函数对此类字符串进行转义,或者在它之前自己添加一个斜杠,如下所示:
'Application\Model\'User'
回答by Hollance
You will find the complete explanation here: http://nl.php.net/manual/en/language.types.string.php
你会在这里找到完整的解释:http: //nl.php.net/manual/en/language.types.string.php
回答by Martin Bean
I think it depends on the context, but it is a good idea to escape backslashes if using it in file paths.
我认为这取决于上下文,但如果在文件路径中使用反斜杠,最好避开反斜杠。
Another good idea is to assign the directory separator to a constant, which I've seen done in various applications before, and use it as thus:
另一个好主意是将目录分隔符分配给一个常量,我之前在各种应用程序中看到过这种做法,并将其用作:
<?php
define('DIRECTORY_SEPARATOR', '\');
echo 'Application'.DIRECTORY_SEPARATOR . 'Models' . DIRECTORY_SEPARATOR . 'User';
?>
If you wish to save space and typing, others use DS
for the constant name.
如果你想节省空间和打字,其他人使用DS
常量名。
<?php
define('DS', '\');
echo 'Application'.DS.'Models'.DS.'User';
?>
This keeps your application portable if you move from a Windows environment to a *nix environment, as you can simple change the directory separator constant to a forward slash.
如果您从 Windows 环境移动到 *nix 环境,这将使您的应用程序保持可移植性,因为您可以简单地将目录分隔符常量更改为正斜杠。
回答by bizna
If it is to be used in an HTML page, then you might as well use code \ to represent a backslash, like so:
如果要在 HTML 页面中使用,那么您不妨使用代码 \ 来表示反斜杠,如下所示:
echo 'Application\Models\User';
It will print:
它会打印:
Application\Models\User
应用程序\模型\用户