PHP - Manual: sapi_windows_vt100_support
2024-11-13
(PHP 7 >= 7.2.0, PHP 8)
sapi_windows_vt100_support — Get or set VT100 support for the specified stream associated to an output buffer of a Windows console.
$stream
, ?bool $enable
= null
): bool
If enable
is null
, the function returns true
if the stream stream
has VT100 control codes enabled, false
otherwise.
If enable
is a bool, the function will try to enable or disable the VT100 features of the stream stream
.
If the feature has been successfully enabled (or disabled), the function will return true
, or false
otherwise.
At startup, PHP tries to enable the VT100 feature of the STDOUT
/STDERR
streams. By the way, if those streams are redirected to a file, the VT100 features may not be enabled.
If VT100 support is enabled, it is possible to use control sequences as they are known from the VT100 terminal. They allow the modification of the terminal's output. On Windows these sequences are called Console Virtual Terminal Sequences.
This function uses the ENABLE_VIRTUAL_TERMINAL_PROCESSING
flag implemented in the Windows 10 API, so the VT100 feature may not be available on older Windows versions.
stream
The stream on which the function will operate.
enable
If bool, the VT100 feature will be enabled (if true
) or disabled (if false
).
If enable
is null
: returns true
if the VT100 feature is enabled, false
otherwise.
If enable
is a bool: 成功时返回 true
, 或者在失败时返回 false
。
版本 | 说明 |
---|---|
8.0.0 |
enable is now nullable.
|
示例 #1 sapi_windows_vt100_support() default state
By default, STDOUT
and STDERR
have the VT100 feature enabled.
php -r "var_export(sapi_windows_vt100_support(STDOUT));echo ' ';var_export(sapi_windows_vt100_support(STDERR));"
以上例程的输出类似于:
true true
By the way, if a stream is redirected, the VT100 feature will not be enabled:
php -r "var_export(sapi_windows_vt100_support(STDOUT));echo ' ';var_export(sapi_windows_vt100_support(STDERR));" 2>NUL
以上例程的输出类似于:
示例 #2 sapi_windows_vt100_support() changing state
You won't be able to enable the VT100 feature of STDOUT
or STDERR
if the stream is redirected.
php -r "var_export(sapi_windows_vt100_support(STDOUT, true));echo ' ';var_export(sapi_windows_vt100_support(STDERR, true));" 2>NUL
以上例程的输出类似于:
true false
示例 #3 Example usage of VT100 support enabled
<?php
$out = fopen('php://stdout','w');
fwrite($out, 'Just forgot a lettr.');
// Moves the cursor two characters backwards
fwrite($out, "\033[2D");
// Inserts one blank, shifting existing text to the right -> Just forgot a lett r.
fwrite($out, "\033[1@");
fwrite($out, 'e');
?>
以上例程会输出:
Just forgot a letter.
官方地址:https://www.php.net/manual/en/function.sapi-windows-vt100-support.php