Php colored console output

How to enable color for PHP CLI?

Enabling color for PHP CLI can be useful for improving the readability of the output on the command line. Here is a step-by-step guide on how to enable color for PHP CLI:

  1. First, make sure that the PHP version you are using supports color output. You can check this by running the following command:

`php -r “echo ‘Color support: ‘.(int)function_exists(‘sapi_windows_vt100_support’);”`

If the output is `Color support: 1`, then your PHP version supports color output. If the output is `Color support: 0`, then you need to upgrade your PHP version.

  1. Next, you need to install the Symfony Console component. This component provides an easy-to-use API for building command-line applications in PHP, including color output. You can install it using Composer:

`composer require symfony/console`

  1. Once you have installed the Symfony Console component, you can start using it to enable color output in your PHP CLI scripts. Here is an example:
Читайте также:  METANIT.COM

“`php
require DIR.’/vendor/autoload.php’;

use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

// create a new ConsoleOutput object
$output = new ConsoleOutput();

// create a new OutputFormatterStyle object and customize the colors
$style = new OutputFormatterStyle(‘red’, ‘yellow’, array(‘bold’, ‘blink’));

// register the new style with the ConsoleOutput object
$output->getFormatter()->setStyle(‘fire’, $style);

// output some text in the new style
$output->writeln(‘Fire!’);
“`

In this example, we create a new `ConsoleOutput` object, which represents the output stream of the PHP CLI script. We then create a new `OutputFormatterStyle` object, which defines the colors for our custom style. We register the new style with the `ConsoleOutput` object, and then use it to output some text in the new style using the `writeln()` method.

  1. You can also use the built-in colors provided by the Symfony Console component. Here is an example:

“`php
require DIR.’/vendor/autoload.php’;

use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

// create a new ConsoleOutput object
$output = new ConsoleOutput();

// output some text in the default red color
$output->writeln(‘Hello, world!’);

// output some text in the default green color
$output->writeln(‘Goodbye, world!’);
“`

In this example, we use the built-in colors provided by the `ConsoleOutput` object. We can use the `fg` option to set the foreground color and the `bg` option to set the background color.

That’s it! With these steps, you can enable color output for your PHP CLI scripts and make them more readable and visually appealing.

Источник

How to Use Colors in Command-line PHP Output

Nothing spruces up command-line output the way colors do. Just like when you’re customizing your [Bash] prompt, you can use color codes in strings to colorize your output. Keep in mind, these colors will only work on the command-line and not in a browser. First let’s take a look at the syntax:

echo "\e[0;31;42mMerry Christmas!\e[0m\n"; 

The above command will echo out “Merry Christmas!” in a red font with a green background. The first part of the string, \e is the escape character and could alternatively be represented by \033 in octal or using the function call chr(27) if you want to close and reopen the string a bunch of times.

Next up is [0;31;42m , this is what sets the foreground color ( 0;31 ) and the background ( 42 ). This is all followed up by the text we’re colorizing. After that is another color declaration that will reset the color to the terminal’s default colors (usually grey text on a black background).

So now that we know how to colorize text, let’s take a look at all of the colors that are available. All in there are 16 different foregrounds and 8 backgrounds. Of the foregrounds, they are split between a normal colors and lighter alternate versions that actually end up being displayed differently depending on the terminal being used. Sometimes they are heavier text, sometimes alternative colors. The colors and codes are as follows:

Foreground Colors

Color Code
Black 0;30
Dark Grey 1;30
Red 0;31
Light Red 1;31
Green 0;32
Light Green 1;32
Brown 0;33
Yellow 1;33
Blue 0;34
Light Blue 1;34
Magenta 0;35
Light Magenta 1;35
Cyan 0;36
Light Cyan 1;36
Light Grey 0;37
White 1;37

Background Colors

It’s worth noting that you actually don’t need to set both the foreground and the background color, if you wanted to you could simply set one or the other using the aforementioned codes. When defining both, you must specify the foreground color first and just separate the codes with a semi-colon.

Good stuff? Want more?

100% Fresh, Grade A Content, Never Spam.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Colorizing console output.

License

alecrabbit/php-console-colour

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Scrutinizer Code Quality Code Coverage

composer require alecrabbit/php-console-colour
$themes = new Themes(); echo $themes->red('This text is red.') . PHP_EOL; echo $themes->underlinedBold('This text is underlined and bold.') . PHP_EOL;

Basically methods of this class just applying corresponding escape sequences to $text

// "\033[2;3mThis text is dark and italic.\033[0m" $colorized = $themes->darkItalic('This text is dark and italic.')

Note: not all methods could be listed.

/** * @method comment(string $text) * @method error(string $text) * @method info(string $text) * * @method yellow(string $text) * @method red(string $text) * @method green(string $text) * @method cyan(string $text) * @method magenta(string $text) * * @method italic(string $text) * @method bold(string $text) * @method dark(string $text) * @method darkItalic(string $text) * @method white(string $text) * @method whiteBold(string $text) * @method underlined(string $text) * @method underlinedBold(string $text) * @method underlinedItalic(string $text) */

Note: new methods will be added. Pull requests are welcomed.

example

example

example

Note: actual colors depend on your terminal color scheme

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Make your PHP command-line application colorful.

License

jfcherng/php-color-output

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

The above screenshot is the output of demo.php . See the Example section.

composer require jfcherng/php-color-output 
Background Foreground Compound Special Alias
b_black f_black f_dark_gray blink b (bold)
b_blue f_blue f_light_blue bold blk (blink)
b_cyan f_brown f_light_cyan dim h (hidden)
b_green f_cyan f_light_green hidden rev (reverse)
b_light_gray f_green f_light_purple reset rst (reset)
b_magenta f_light_gray f_light_red reverse u (underline)
b_red f_normal f_white underline
b_yellow f_purple f_yellow
f_red
 /** * Make a string colorful. * * @param string $str the string * @param string|string[] $colors the colors * @param bool $reset reset color at the end of the string? * * @return string the colored string */ \Jfcherng\Utility\CliColor::color(string $str, $colors = [], bool $reset = true): string /** * Remove all colors from a string. * * @param string $str the string * * @return string the string without colors */ \Jfcherng\Utility\CliColor::noColor(string $str): string
 include __DIR__ . '/vendor/autoload.php'; use \Jfcherng\Utility\CliColor; // colors in a string using a comma as the delimiter echo CliColor::color('foo', 'f_light_cyan, b_yellow'); // "\033[1;36;43mfoo\033[0m" echo PHP_EOL; // colors in an array echo CliColor::color('foo', ['f_white', 'b_magenta']); // "\033[1;37;45mfoo\033[0m" echo PHP_EOL; // do not auto reset color at the end of string echo CliColor::color('foo', ['f_red', 'b_green', 'b', 'blk'], false); // "\033[31;42;1;5mfoo" // manually add color reset echo CliColor::color('', 'reset'); // "\033[0m" echo PHP_EOL; // remove all color codes from a string echo CliColor::noColor("\033[31;42;5mfoo\033[0mbar"); // "foobar" echo PHP_EOL;

About

Make your PHP command-line application colorful.

Источник

Оцените статью