\e的意图是什么

发布于 2024-11-17 23:17:24 字数 378 浏览 7 评论 0原文

我想知道这个转义序列。 PHP 手册\e 代表“转义” 好吧,我用谷歌搜索了一下,发现了ANSI Escape Sequences, 我不知道这个 \e 是否指的是他们。

来自维基百科:

ANSI 转义序列是嵌入文本中的字符,用于控制视频文本终端上的格式、颜色和其他输出选项。

但我看不出这个转义序列和 PHP 如何交互。 不知道发的所有内容是否正确。

有人可以谈谈这个话题并举例说明吗?

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.

From Wikipedia:

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

But i cant see how this escape sequences and PHP can interact.
I don't know if all the things that post are correct.

Can someone talk about this topic and show examples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弱骨蛰伏 2024-11-24 23:17:24

PHP 不能仅用于 Web 服务器; PHP 脚本可以从命令行运行,如下所示:

$ php foo.php

如果创建一个从命令行运行的 PHP 脚本(例如 CakePHP 附带的 cake 命令),则 \e当您想在终端模拟器中进行颜色和格式设置时, 会派上用场。

您可以在此页面上找到有关这些转义序列的更多信息。

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

$ 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.

You can find more about these escape sequences on this page.

白芷 2024-11-24 23:17:24

PHP 使用字符序列 \e 来表示 ESC 字符、ASCII 格式的 0x1B(以及 UTF-8 和其他 ASCII 兼容编码)。有一种方法可以用可打印字符(即 \e,在本例中),因为用标准键盘编写这些字符并不容易(更不用说在屏幕上读取它们了)。这些序列通常称为“转义序列”。

您链接到的手册页只是简单地说“如果您正在编写正则表达式并希望匹配 ESC 字符,则可以使用 \e 来执行此操作其他 \... 序列同样表示难以直接输入的字符和字符集,

ESC 有许多用途,其中包括 ANSI 转义序列。 ,用于修改命令行程序的输出,通常用于添加颜色,例如as \e[...m 其中 ... 是一个或多个 选择图形呈现代码(在该页面上搜索“SRG”)。这不是 PHP 特有的;它是终端,而不是执行程序,可以理解这些转义序列。 (用任何语言编写)输出适当的字节序列兼容的终端将触发此行为,

您可以在 Bash 中轻松看到此行为,尝试在终端中运行以下命令:

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

大多数终端会将第一个命令的输出设置为红色,第二个命令的输出设置为绿色背景。然而,许多终端故意不支持闪烁文本,出于同样的原因,HTML 标签通常不受支持 - 这很烦人:)

不用说,术语“ “转义序列”在许多不同的上下文中使用,含义略有不同。但一般来说,转义序列是一些特殊的字符序列,除了字符本身之外还具有特定的含义。

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".

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.

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.

You can see this behavior easily in Bash, try running 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 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 :)

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.

Bonjour°[大白 2024-11-24 23:17:24

有很多逃脱的方法。

\b:退格键
\n:换行
\r:硬返回
\e:转义
等等...

它们在输出时都会做不同的事情。

There are many escapes.

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

They all do different things when they are output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文