如何在PHP CLI中检测STDOUT重定向? [英] How can I detect STDOUT redirection in PHP CLI?

查看:92
本文介绍了如何在PHP CLI中检测STDOUT重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 being redirected (eg to a log file).

到目前为止,我可以找到方法来检测除PHP以外的所有内容中的STDOUT重定向...所以任何人都可以告诉我它在PHP中是如何完成的。 ?

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!";

这篇关于如何在PHP CLI中检测STDOUT重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆