I was wondering about this escape sequence. The PHP Manual says that \e
represents "escape" Well, i've googled it and found about ANSI Escape Sequences, i dont know if this \e
refers them.
我想知道这个逃脱序列。 PHP手册说,\ e表示“逃避”嗯,我用Google搜索并发现有关ANSI Escape序列,我不知道这是否引用它们。
From Wikipedia:
来自*:
ANSI escape sequences are characters embedded in the text used to control formatting, color, and other output options on video text terminals.
ANSI转义序列是嵌入在文本中的字符,用于控制视频文本终端上的格式,颜色和其他输出选项。
But i cant see how this escape sequences and PHP can interact. I don't know if all the things that post are correct.
但是我无法看到这个转义序列和PHP如何交互。我不知道发布的所有内容是否正确。
Can someone talk about this topic and show examples?
有人可以谈论这个话题并展示例子吗?
4 个解决方案
#1
8
PHP cannot only be used with a web server; PHP scripts can run from the command line, like this:
PHP不仅可以与Web服务器一起使用; PHP脚本可以从命令行运行,如下所示:
$ php foo.php
If one makes a PHP script which is meant to run from the command line (e.g. the cake
command which ships with CakePHP), \e
can come in handy when you want to do colors and formatting in terminal emulators.
如果一个PHP脚本是从命令行运行的(例如CakePHP附带的cake命令),当你想在终端模拟器中进行颜色和格式化时,\ e可以派上用场。
You can find more about these escape sequences on this page.
您可以在此页面上找到有关这些转义序列的更多信息。
#2
0
You are looking at the manual for regular expressions (like preg_replace
). So the question how PHP can interact with them is quite simple: if you have a text and you want to find, replace, match etc a string then an escape character can be "interacted" with just like any other character.
您正在查看正则表达式的手册(如preg_replace)。因此,PHP如何与它们进行交互的问题非常简单:如果你有一个文本,并且你想要查找,替换,匹配等一个字符串,那么转义字符可以像任何其他字符一样“交互”。
#3
0
There are many escapes.
有许多逃脱。
\b
: backspace\n
: new line\r
: hard return\e
: escape
etcetera...
\ b:退格\ n:新行\ r:硬回复\ e:转义等等...
They all do different things when they are output.
它们在输出时都会做不同的事情。
#4
0
The sequence of characters \e
is used by PHP to represent the ESC
character, 0x1B
in ASCII (and UTF-8 and other ASCII-compatible encodings). It's helpful to have a way to represent non-printing characters with printable characters (namely \
and e
, in this case) because it's not easy to write such characters with a standard keyboard (let alone read them on the screen). These sequences are generally called "escape sequences".
PHP使用字符序列\ e来表示ESC字符,ASCII(以及UTF-8和其他ASCII兼容编码)中的0x1B。有一种方法可以用可打印的字符(即在这种情况下为\和e)来表示非打印字符,因为使用标准键盘编写这些字符并不容易(更不用说在屏幕上阅读它们)。这些序列通常称为“逃逸序列”。
The manual page you are linking to is simply saying "if you're writing a regular expression and want to match on the ESC
character, you can use \e
to do so. The other \...
sequences similarly represent characters and sets of characters that are hard to directly input.
您链接到的手册页只是说“如果您正在编写正则表达式并希望匹配ESC字符,则可以使用\ e来执行此操作。其他\ ...序列同样表示字符和集合难以直接输入的字符。
As you discovered, ESC
has many uses, among them ANSI escape sequences, which are used to modify the output of a command-line program, commonly to add colors, such as \e[...m
where ...
is one or more Select Graphic Rendition codes (search for "SRG" on that page). This isn't specific to PHP; it is the terminal, not the executing program, that understands these escape sequences. Any program (written in any language) that outputs the appropriate sequences of bytes in a compatible terminal will trigger this behavior.
正如您所发现的,ESC有许多用途,其中有ANSI转义序列,用于修改命令行程序的输出,通常用于添加颜色,例如\ e [... m其中...是一个或者更多选择图形再现代码(在该页面上搜索“SRG”)。这不是PHP特有的;理解这些转义序列是终端,而不是执行程序。在兼容终端中输出适当字节序列的任何程序(用任何语言编写)都会触发此行为。
You can see this behavior easily in Bash, try running the following commands in your terminal:
您可以在Bash中轻松查看此行为,尝试在终端中运行以下命令:
$ echo -e '\e[31mRED TEXT\e[m'
$ echo -e '\e[42mGREEN BACKGROUND\e[m'
$ echo -e '\e[5mBLINKING?\e[m'
Most terminals will style the output of the first command in red, and the second in a green background. However many terminals intentionally do not support blinking text, for the same reason the HTML <blink>
tag is generally unsupported - it's annoying :)
大多数终端将第一个命令的输出设置为红色,第二个输出设置为绿色背景。然而,许多终端故意不支持闪烁文本,出于同样的原因,HTML
Needless to say, the term "escape sequence" is used in a number of different contexts to mean slightly different things. But in general an escape sequence is some special sequence of characters that has a specific meaning beyond just the characters themselves.
不用说,术语“转义序列”在许多不同的上下文中用于表示略微不同的事物。但一般来说,转义序列是一些特殊的字符序列,它们具有除字符本身之外的特定含义。
#1
8
PHP cannot only be used with a web server; PHP scripts can run from the command line, like this:
PHP不仅可以与Web服务器一起使用; PHP脚本可以从命令行运行,如下所示:
$ php foo.php
If one makes a PHP script which is meant to run from the command line (e.g. the cake
command which ships with CakePHP), \e
can come in handy when you want to do colors and formatting in terminal emulators.
如果一个PHP脚本是从命令行运行的(例如CakePHP附带的cake命令),当你想在终端模拟器中进行颜色和格式化时,\ e可以派上用场。
You can find more about these escape sequences on this page.
您可以在此页面上找到有关这些转义序列的更多信息。
#2
0
You are looking at the manual for regular expressions (like preg_replace
). So the question how PHP can interact with them is quite simple: if you have a text and you want to find, replace, match etc a string then an escape character can be "interacted" with just like any other character.
您正在查看正则表达式的手册(如preg_replace)。因此,PHP如何与它们进行交互的问题非常简单:如果你有一个文本,并且你想要查找,替换,匹配等一个字符串,那么转义字符可以像任何其他字符一样“交互”。
#3
0
There are many escapes.
有许多逃脱。
\b
: backspace\n
: new line\r
: hard return\e
: escape
etcetera...
\ b:退格\ n:新行\ r:硬回复\ e:转义等等...
They all do different things when they are output.
它们在输出时都会做不同的事情。
#4
0
The sequence of characters \e
is used by PHP to represent the ESC
character, 0x1B
in ASCII (and UTF-8 and other ASCII-compatible encodings). It's helpful to have a way to represent non-printing characters with printable characters (namely \
and e
, in this case) because it's not easy to write such characters with a standard keyboard (let alone read them on the screen). These sequences are generally called "escape sequences".
PHP使用字符序列\ e来表示ESC字符,ASCII(以及UTF-8和其他ASCII兼容编码)中的0x1B。有一种方法可以用可打印的字符(即在这种情况下为\和e)来表示非打印字符,因为使用标准键盘编写这些字符并不容易(更不用说在屏幕上阅读它们)。这些序列通常称为“逃逸序列”。
The manual page you are linking to is simply saying "if you're writing a regular expression and want to match on the ESC
character, you can use \e
to do so. The other \...
sequences similarly represent characters and sets of characters that are hard to directly input.
您链接到的手册页只是说“如果您正在编写正则表达式并希望匹配ESC字符,则可以使用\ e来执行此操作。其他\ ...序列同样表示字符和集合难以直接输入的字符。
As you discovered, ESC
has many uses, among them ANSI escape sequences, which are used to modify the output of a command-line program, commonly to add colors, such as \e[...m
where ...
is one or more Select Graphic Rendition codes (search for "SRG" on that page). This isn't specific to PHP; it is the terminal, not the executing program, that understands these escape sequences. Any program (written in any language) that outputs the appropriate sequences of bytes in a compatible terminal will trigger this behavior.
正如您所发现的,ESC有许多用途,其中有ANSI转义序列,用于修改命令行程序的输出,通常用于添加颜色,例如\ e [... m其中...是一个或者更多选择图形再现代码(在该页面上搜索“SRG”)。这不是PHP特有的;理解这些转义序列是终端,而不是执行程序。在兼容终端中输出适当字节序列的任何程序(用任何语言编写)都会触发此行为。
You can see this behavior easily in Bash, try running the following commands in your terminal:
您可以在Bash中轻松查看此行为,尝试在终端中运行以下命令:
$ echo -e '\e[31mRED TEXT\e[m'
$ echo -e '\e[42mGREEN BACKGROUND\e[m'
$ echo -e '\e[5mBLINKING?\e[m'
Most terminals will style the output of the first command in red, and the second in a green background. However many terminals intentionally do not support blinking text, for the same reason the HTML <blink>
tag is generally unsupported - it's annoying :)
大多数终端将第一个命令的输出设置为红色,第二个输出设置为绿色背景。然而,许多终端故意不支持闪烁文本,出于同样的原因,HTML
Needless to say, the term "escape sequence" is used in a number of different contexts to mean slightly different things. But in general an escape sequence is some special sequence of characters that has a specific meaning beyond just the characters themselves.
不用说,术语“转义序列”在许多不同的上下文中用于表示略微不同的事物。但一般来说,转义序列是一些特殊的字符序列,它们具有除字符本身之外的特定含义。