What is the intention \ e

I was curious about this escape sequence. The PHP manual says that \e represents "escape" Well, I searched for it and found about ANSI Escape Sequences , I don’t know if this refers to \e .

From Wikipedia:

ANSI escape sequences are characters embedded in text that are used to control the formatting, color, and other output options on video text terminals.

But I do not see how these escape sequences and PHP can interact. I do not know if all that the message is correct.

Can anyone talk about this topic and show examples?

+6
source share
4 answers

PHP cannot be used only with a web server; PHP scripts can be run from the command line, for example:

 $ php foo.php 

If you are creating a PHP script that is designed to be run from the command line (for example, the cake command that comes with CakePHP), \e can come in handy when you want to make colors and format emulators in the terminal.

More information about these escape sequences can be found on this page .

+8
source

You are looking at the manual for regular expressions (e.g. preg_replace ). So the question of how PHP can interact with them is pretty simple: if you have text and you want to find, replace, match, etc. A string, then the escape character can be "interacting" just like any other character.

0
source

There are many screens.

\b : backspace
\n : new line
\r : hard return
\e : escape
etc...

They all do different things when they are deduced.

0
source

The \e character sequence is used by PHP to represent the ESC character, 0x1B in ASCII (and UTF-8 and other ASCII-compatible encodings). This is useful to represent non-printable characters with printable characters (namely \ and e , in this case), because it is not easy to write such characters with a standard keyboard (not to mention reading them on the screen). These sequences are commonly called "escape sequences."

The manual page that you are referencing simply says: β€œIf you write a regular expression and want to match an ESC character, you can use \e to do this. Other sequences \... represent characters and character sets that are hard to type directly.

As you have discovered, ESC has many uses, including ANSI escape sequences, which are used to change the output of a program from the command line, usually to add colors, for example \e[...m , where ... is one or more Select graphic coding codes (find "SRG" on this page). This does not apply to PHP; it is a terminal, not a runtime program, understands these escape sequences. Any program (written in any language) that outputs the corresponding sequence of bytes in a compatible terminal causes this behavior.

You can easily see this behavior in Bash, try the following commands in your terminal:

 $ echo -e '\e[31mRED TEXT\e[m' $ echo -e '\e[42mGREEN BACKGROUND\e[m' $ echo -e '\e[5mBLINKING?\e[m' 

Most terminals will stylize 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> usually not supported - it is annoying :)

Needless to say, the term "escape sequence" is used in several different contexts to mean slightly different things. But in general, an escape sequence is some special character sequence that has a specific meaning besides the characters themselves.

0
source

Source: https://habr.com/ru/post/891586/


All Articles