jfcherng/php-color-output

让你的 PHP 命令行应用程序更丰富多彩。

3.0.0 2021-05-27 02:45 UTC

This package is auto-updated.

Last update: 2024-08-30 01:09:02 UTC


README

GitHub Workflow Status (branch) Packagist Packagist Version Project license GitHub stars Donate to this project using Paypal

demo.gif

上面的截图是 demo.php 的输出。请参阅 示例 部分。

安装

composer require jfcherng/php-color-output

可用样式

函数和方法

<?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

示例

<?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;