在 PHP 的 str_replace() 中,双反斜杠是什么意思? [英] In PHP's str_replace(), what does the double backslash mean?
问题描述
我想删除所有 \r
\n
\r\n
这很容易,所以我写道:
I want to remove all \r
\n
\r\n
which is pretty easy to so I wrote:
str_replace(array("\r","\n"),"",$text);
但我看到了这一行:
str_replace(array("\r","\n","\\r","\\n"),"",$text);
我想知道双反斜杠是什么意思\\r
和\\n
.
and I was wondering what is the double backslash means \\r
and \\n
.
推荐答案
\
是一个转义符,用于对后面的字符进行转义.
\
is an escape character, it's used to escape the following character.
在"\n"
中,反斜杠转义n
,结果将是一个换行字符.
In "\n"
, the backslash escapes n
and the result will be a new line character.
在"\\n"
中,第一个反斜杠转义第二个反斜杠,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).
参见PHP官方文档>字符串.
在您的问题的上下文中,str_replace()
将删除新行("\n"
和 "\r"
)和还要从字符串中删除 \n
和 \r
("\\n"
and "\\r"
分别).文本没有理由包含单词 \n
和 \r
,所以似乎使用 "\\n"
和 "\\r"
对这里没有兴趣.
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). There's no reason a text contains the words \n
and \r
, so it seems that using "\\n"
and "\\r"
has no interest here.
这篇关于在 PHP 的 str_replace() 中,双反斜杠是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!