| 1 |
# php-color-output |
| 2 |
|
| 3 |
[](https://github.com/jfcherng/php-color-output/actions) |
| 4 |
[](https://packagist.org/packages/jfcherng/php-color-output) |
| 5 |
[](https://packagist.org/packages/jfcherng/php-color-output) |
| 6 |
[](https://github.com/jfcherng/php-color-output/blob/master/LICENSE) |
| 7 |
[](https://github.com/jfcherng/php-color-output/stargazers) |
| 8 |
[](https://www.paypal.me/jfcherng/5usd) |
| 9 |
|
| 10 |
 |
| 11 |
|
| 12 |
The above screenshot is the output of `demo.php`. See the [Example](#example) section. |
| 13 |
|
| 14 |
|
| 15 |
## Installation |
| 16 |
|
| 17 |
```text |
| 18 |
composer require jfcherng/php-color-output |
| 19 |
``` |
| 20 |
|
| 21 |
|
| 22 |
## Available Styles |
| 23 |
|
| 24 |
| Background | Foreground | Compound | Special | Alias | |
| 25 |
| --- | --- | --- | --- | --- | |
| 26 |
| b_black | f_black | f_dark_gray | blink | b (bold) | |
| 27 |
| b_blue | f_blue | f_light_blue | bold | blk (blink) | |
| 28 |
| b_cyan | f_brown | f_light_cyan | dim | h (hidden) | |
| 29 |
| b_green | f_cyan | f_light_green | hidden | rev (reverse) | |
| 30 |
| b_light_gray | f_green | f_light_purple | reset | rst (reset) | |
| 31 |
| b_magenta | f_light_gray | f_light_red | reverse | u (underline) | |
| 32 |
| b_red | f_normal | f_white | underline | - | |
| 33 |
| b_yellow | f_purple | f_yellow | - | - | |
| 34 |
| - | f_red | - | - | - | |
| 35 |
|
| 36 |
|
| 37 |
## Functions and Methods |
| 38 |
|
| 39 |
```php |
| 40 |
<?php |
| 41 |
|
| 42 |
/** |
| 43 |
* Make a string colorful. |
| 44 |
* |
| 45 |
* @param string $str the string |
| 46 |
* @param string|string[] $colors the colors |
| 47 |
* @param bool $reset reset color at the end of the string? |
| 48 |
* |
| 49 |
* @return string the colored string |
| 50 |
*/ |
| 51 |
\Jfcherng\Utility\CliColor::color(string $str, $colors = [], bool $reset = true): string |
| 52 |
|
| 53 |
/** |
| 54 |
* Remove all colors from a string. |
| 55 |
* |
| 56 |
* @param string $str the string |
| 57 |
* |
| 58 |
* @return string the string without colors |
| 59 |
*/ |
| 60 |
\Jfcherng\Utility\CliColor::noColor(string $str): string |
| 61 |
``` |
| 62 |
|
| 63 |
|
| 64 |
## Example |
| 65 |
|
| 66 |
```php |
| 67 |
<?php |
| 68 |
|
| 69 |
include __DIR__ . '/vendor/autoload.php'; |
| 70 |
|
| 71 |
use \Jfcherng\Utility\CliColor; |
| 72 |
|
| 73 |
// colors in a string using a comma as the delimiter |
| 74 |
echo CliColor::color('foo', 'f_light_cyan, b_yellow'); // "\033[1;36;43mfoo\033[0m" |
| 75 |
|
| 76 |
echo PHP_EOL; |
| 77 |
|
| 78 |
// colors in an array |
| 79 |
echo CliColor::color('foo', ['f_white', 'b_magenta']); // "\033[1;37;45mfoo\033[0m" |
| 80 |
|
| 81 |
echo PHP_EOL; |
| 82 |
|
| 83 |
// do not auto reset color at the end of string |
| 84 |
echo CliColor::color('foo', ['f_red', 'b_green', 'b', 'blk'], false); // "\033[31;42;1;5mfoo" |
| 85 |
|
| 86 |
// manually add color reset |
| 87 |
echo CliColor::color('', 'reset'); // "\033[0m" |
| 88 |
|
| 89 |
echo PHP_EOL; |
| 90 |
|
| 91 |
// remove all color codes from a string |
| 92 |
echo CliColor::noColor("\033[31;42;5mfoo\033[0mbar"); // "foobar" |
| 93 |
|
| 94 |
echo PHP_EOL; |
| 95 |
``` |