jfcherng / php-color-output
Make your PHP command-line application colorful.
Fund package maintenance!
www.paypal.me/jfcherng/5usd
Installs: 2 749 006
Dependents: 2
Suggesters: 0
Security: 0
Stars: 12
Watchers: 3
Forks: 3
Open Issues: 0
Requires
- php: >=7.1.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.19
- liip/rmt: ^1.6
- phan/phan: ^2 || ^3 || ^4
- phpunit/phpunit: >=7 <10
- squizlabs/php_codesniffer: ^3.5
README
The above screenshot is the output of demo.php
. See the Example section.
Installation
composer require jfcherng/php-color-output
Available Styles
Functions and Methods
<?php /** * 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
Example
<?php 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;