lehasi4184
lehasi4184

Reputation: 23

PHP Detect if current stdout support ANSI escape chars

Let's have following script:

echo "Hello world\e[5Dmoon!";

Using PHP_SAPI I am able to determine if script is running on CLI or on WEB.

If script is executed from command line, it will output Hello moon!. But output of script can be forwarded to e.g. text file, like php test.php > output.txt. And if output is forwarded, escape chars won't work and output will be Hello world^[[5Dmoon.

How to determine, if current stdout supports ANSI escape chars, to avoid "spamming" output which does not support this?

Upvotes: 1

Views: 259

Answers (1)

Daniel Vítek
Daniel Vítek

Reputation: 185

The function you are searching for should be stream_isatty(STDOUT).

It returns correct values for your use case - web false, terminal true, output in TXT false.

Upvotes: 1

Related Questions