开发者

How can I detect STDOUT redirection in PHP CLI?

开发者 https://www.devze.com 2023-02-27 01:31 出处:网络
I\'ve a PHP CLI script that uses shell escape sequences for bolding, but I want to be able to disable these automatically when the script\'s 开发者_C百科being redirected (eg to a log file).

I've a PHP CLI script that uses shell escape sequences for bolding, but I want to be able to disable these automatically when the script's 开发者_C百科being redirected (eg to a log file).

I can find ways to detect STDOUT redirection in everything but PHP so far... so can anyone tell me how it is done in PHP?


This should give you what you want:

if(posix_isatty(STDOUT))
    echo "No Redirection";
else
    echo "Redirection!";


For Windows, you can use this:

$b = stream_isatty(STDIN);
if ($b) {
   echo "no redirect\n";
} else {
   echo "redirect\n";
}

https://php.net/function.stream-isatty

0

精彩评论

暂无评论...
验证码 换一张
取 消