本文介绍了为什么不是“\t”逃避这个PHP日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是日期。
一切正在被转义,但在\a\t中的t。任何人都知道为什么?

Here's the date.Everything is being escaped but the t in "\a\t". Anybody know why?

date("M m\, Y \a\t g\:ia", $s->post_date);


推荐答案

\t 是水平制表符的转义序列。

"\t" is a escape sequence for the horizontal tab character.

使用'\t'\\t

单引号字符串解释 \ 字面上,我建议你的用例。否则,你必须逃避 \ 字符以字面解释。

Single-quoted strings interpret \ literally, which I'd recommend for your use case. Otherwise you have to escape the \ character to have it interpreted literally.

在PHP的情况下,$在双引号字符串中的无效转义序列之前的c $ c> \ 也可以字面上解释。我宁愿避免这种行为,遵循最不重要的原则。

In PHP's case, the \ preceding an invalid escape sequence inside a double-quoted string also gets interpreted literally. I'd rather avoid this behavior, following the principle of least surprise.

ps。 (感谢@IMSoP)有两种情况,其中 \ s在单引号字符串中不会直接解释:

ps. (thanks to @IMSoP) There are two cases where \s are not interpreted literally inside single-quoted strings:


  • 双重反斜杠仍然可以但可选。例如:'\\hi'==='\hi'

  • 字符串分隔符必须在一个字符串文字。例如:'\''==='

  • Doubling backslashes is still possible but optional. E.g.: '\\hi' === '\hi'
  • The string delimiter character must be escaped inside of a string literal. E.g.: '\'' === "'"

单引号不太奇怪,因为 \\\
\r \t \v \040 类似的结果在实际字符串文字中的字符序列,而不是这些字符串解释为转义序列。

Still, single-quoted strings are less surprising in that \n, \r, \t, \v, \040 and similar result in the actual sequence of characters inside of the string literal instead of these being interpreted as escape sequences.

将所有必须从字面上解释的反斜杠加倍也是一个坚固的选项,与双重和单引号字符串。

Doubling all backslashes that must be interpreted literally is also a sturdy option that works with both double and single quoted strings as well.

这篇关于为什么不是“\t”逃避这个PHP日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 09:27