3

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
  • @bcsanches Yup - lost track of which site was which. Thanks! Commented Apr 20, 2011 at 13:32

2 Answers 2

3

This should give you what you want:

if(posix_isatty(STDOUT))
    echo "No Redirection";
else
    echo "Redirection!";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - exactly what I was trying to find out. Works too (but you knew that). To try and ensure it wouldn't bail in windows or non-posix systems, I've also called is_function('posix_isatty') before trying the test.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.