[英]In PHP's str_replace(), what does the double backslash mean?
I want to remove all \\r
\\n
\\r\\n
which is pretty easy to so I wrote:我想删除所有
\\r
\\n
\\r\\n
这很容易,所以我写道:
str_replace(array("\r","\n"),"",$text);
but I saw this line:但我看到了这一行:
str_replace(array("\r","\n","\\r","\\n"),"",$text);
and I was wondering what is the double backslash means \\\\r
and \\\\n
.我想知道双反斜杠是什么意思
\\\\r
和\\\\n
。
\\
is an escape character, it's used to escape the following character. \\
是一个转义字符,用于转义后面的字符。
In "\\n"
, the backslash escapes n
and the result will be a new line character.在
"\\n"
,反斜杠转义n
并且结果将是一个新行字符。
In "\\\\n"
, the first backslash escapes the second backslash and the n
is kept as is, so the result is a string containing \\n
(literally).在
"\\\\n"
,第一个反斜杠转义第二个反斜杠并且n
保持原样,因此结果是一个包含\\n
(字面意思)的字符串。
See the PHP official documentation > Strings .请参阅PHP 官方文档 > 字符串。
In the context of your question, str_replace()
will remove new lines ( "\\n"
and "\\r"
) and also remove \\n
and \\r
from the string ( "\\\\n"
and "\\\\r"
respectively).在您的问题的上下文中,
str_replace()
将删除新行( "\\n"
和"\\r"
)并从字符串中删除\\n
和\\r
(分别为"\\\\n"
和"\\\\r"
) . There's no reason a text contains the words \\n
and \\r
, so it seems that using "\\\\n"
and "\\\\r"
has no interest here.文本没有理由包含单词
\\n
和\\r
,所以似乎在这里使用"\\\\n"
和"\\\\r"
没有兴趣。
The first backslash escapes the second one, so it matches a literal backslash in $text
.第一个反斜杠转义第二个,因此它匹配
$text
中的文字反斜杠。
I'm not sure why you would want to match that if you just want to remove newlines and carriage returns from the string.如果您只想从字符串中删除换行符和回车符,我不确定为什么要匹配它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.