Skip to content

Instantly share code, notes, and snippets.

@dannykopping
Forked from superbrothers/ansi-color.php
Created June 15, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannykopping/f730a0bf1e2231aa0f76a59ba209acad to your computer and use it in GitHub Desktop.
Save dannykopping/f730a0bf1e2231aa0f76a59ba209acad to your computer and use it in GitHub Desktop.
ANSI color terminal output module for php
<?php
/**
* php-ansi-color
*
* Original
* https://github.com/loopj/commonjs-ansi-color
*
* @code
* <?php
* require_once "ansi-color.php";
*
* use PhpAnsiColor\Color;
*
* // Print the word "Error" to stdout in red
* error_log(Color::set("Error", "red"));
*
* // Print the word "Error" in red and underlined
* error_log(Color::set("Error", "red+underline"));
*
* // Print the word "Success" in bold green, followed by a message
* error_log(Color::set("Success", "green+bold"), "Something was successful!");
* @endcode
*/
namespace PhpAnsiColor;
class Color
{
protected static $ANSI_CODES = array(
"off" => 0,
"bold" => 1,
"italic" => 3,
"underline" => 4,
"blink" => 5,
"inverse" => 7,
"hidden" => 8,
"black" => 30,
"red" => 31,
"green" => 32,
"yellow" => 33,
"blue" => 34,
"magenta" => 35,
"cyan" => 36,
"white" => 37,
"black_bg" => 40,
"red_bg" => 41,
"green_bg" => 42,
"yellow_bg" => 43,
"blue_bg" => 44,
"magenta_bg" => 45,
"cyan_bg" => 46,
"white_bg" => 47
);
public static function set($str, $color)
{
$color_attrs = explode("+", $color);
$ansi_str = "";
foreach ($color_attrs as $attr) {
$ansi_str .= "\033[" . self::$ANSI_CODES[$attr] . "m";
}
$ansi_str .= $str . "\033[" . self::$ANSI_CODES["off"] . "m";
return $ansi_str;
}
public static function log($message, $color)
{
error_log(self::set($message, $color));
}
public static function replace($full_text, $search_regexp, $color)
{
$new_text = preg_replace_callback(
"/($search_regexp)/",
function ($matches) use ($color) {
return static::set($matches[1], $color);
},
$full_text
);
return is_null($new_text) ? $full_text : $new_text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment