Help us understand the problem. What is going on with this article?

PHP | ANSI Escape Sequencesを利用する

More than 5 years have passed since last update.

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした