在 PHP 的 str_replace() 中,双反斜杠是什么意思? [英] In PHP's str_replace(), what does the double backslash mean?

查看:24
本文介绍了在 PHP 的 str_replace() 中,双反斜杠是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除所有 \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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆