LoginSignup
4
5

More than 5 years have passed since last update.

PHP | ANSI Escape Sequencesを利用する

Posted at

PHPでANSI Escape Sequencesを利用する

概要

PHPでANSI Escape Sequenceを利用して、コンソールに色つき文字を出力します。
ANSI Escape Sequencesについては説明略。

サンプルコード

class AnsiEscapeSequences {
  static function red($text) {
    return static::color($text, '31');
  }

  static function blue($text) {
    return static::color($text, '34');
  }

  static function yellow($text) {
    return static::color($text, '33');
  }

  static function color($text, $color) {
    return "\033[".$color.";1m".$text."\033[0m";
  }
}

print(AnsiEscapeSequences::red('red')."\n");
print(AnsiEscapeSequences::blue('blue')."\n");
print(AnsiEscapeSequences::yellow('yellow')."\n");
print(AnsiEscapeSequences::color('green', '32')."\n");

出力

画面上

red
blue
yellow
green

実際の文字列

[31;1mred[0m
[34;1mblue[0m
[33;1myellow[0m
[32;1mgreen[0m

出力画像

ansi_escape_php.png

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5