Giter Club home page Giter Club logo

jfcherng-php-color-output's Introduction

php-color-output

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

demo.gif

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

Installation

composer require jfcherng/php-color-output

Available Styles

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

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;

jfcherng-php-color-output's People

Contributors

brianteeman avatar jfcherng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.