开发者

How can I detect STDOUT redirection in PHP CLI?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜