问题
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).
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?
回答1:
This should give you what you want:
if(posix_isatty(STDOUT))
echo "No Redirection";
else
echo "Redirection!";
来源:https://stackoverflow.com/questions/5730268/how-can-i-detect-stdout-redirection-in-php-cli