Hello!
I wanted to ask if someone ever managed to give the output of a PHP script on commandline a different color? Like highlighted syntax.
I know there is a syntax for colors on bash, and I know you can use Windows Powershell to echo different colored strings, but how can I use them in PHP? I mean when I echo the color syntax for bash it will only display the syntax, but not execute it. Same goes for Powershell commands like "Write-Host -ForegroundColor Red "redtext"".
I've already tried fwrite(STDOUT, 'Write-Host -ForegroundColor Red "redtext"') too, but that doesnt work either. And I cant use exec or alike because it doesnt show me the output directly on the commandline I started the php script from.
I'm really stuck here, although I'm sure someone else did on the same problem before and maybe has a workaround?

To conclude:
I want to use the following PHP code
echo "text <code>text</code> text";
on any OS you want, which gives me the output
text text text
while the text in the middle is colored. I just dont know what "code" I need.

Thanks for reading,
Reiners

    I do not believe there is any built-in ability for this within PHP. There might be ways to interface with each different OS via exec()-type commands, but I'm not sure how you'd go about that (other than it would probably require a switch or series of if/else constructs to account for each different OS.

    An alternative might be the PHP-GTK extension, which I believe could do what you want, but it would probably require that the extension be installed on each client that runs the script.

      I'm the only one who uses the script, so that should be no problem. And I can use any OS you want, but I cant find a way to color the PHP commandline output. I was thinking of configuring the bash/cmd/powershell itself, so that anything matching a special pattern gets displayed green (that would be sufficient), but I couldn't find out how to do this on each of those commandlinetools ...

      I'll take a look at GTK again, but as far as I remember I didnt like it 😉

        Just to cover all bases: is there any reason you couldn't just run it as a "localhost" web application, using HTML to do all your formatting, running under a local Apache or IIS web server?

          actually because I copy the script to different locations (outside the webdirectory) where it does some checks on all subdirectories and its files. It is possible to do this via webserver and browser, but I prefer the command line. But maybe I just didnt choose the right language for this 😉

            with a team.php script that i have for my hockey teams what ever team id shows up and what ever colors I have specified the colors for that team show up for text and tables... not sure if its what your looking for, but goto cmhl.ca/league or theufhl.com and look at the teams. if its kind of what you need I will post what I have for you..

              😉 no sry, I would like to have colors on my command line, not on a webpage.

                If I was to do something like this (I have not done so before, however) I would create a shell script that invoked php and then modified the output on the fly. This link may help with the colorizing.

                  Yeah, I was going to suggest something with a shell script that hooked the php application, because it's most likely the only way you're going to do it. Is there a reason you're using PHP in particular instead of something like just plain shell scripting? What sort of operations are you doing? They may be faster in a shell script than in PHP.

                    the script traces some defined variables and functions in other php scripts recursively, mainly. since it then outputs me the found lines (PHP code) it would be nice to have a highlighted syntax, or at least some colors for predefined regex patterns to mark all variables or something like that ...
                    I'm going to try to find out how to do that via shell script, if possible ... thanks so far.

                      You certainly could do it in a shell script, using some amalgamation of find and grep, I'd imagine. And then Valdhor posted a link on colorizing, so it shouldn't be too impossibly difficult. 😛

                        Write a Reply...