Introduction

Introduction – How to use Console_Color

Basics

Console_Color provides methods to convert complete strings tagged with special markers into ANSI-compatible color code representations, and methods that directly return those codes for certain colors.

The method used most often is convert(). It takes a string, e.g.

%bHello World!%n\n

or

3 out of 4 people make up about %r75%% %n

and returns the ANSI representation. The characters following a percentage sign (%) do have special meanings. See color codes to get an overview.

To reset the colors to normal, use %n.

Further, there is an escape() method that prevents special chars from being treated as markers.

Methods bgcolor(), color(), fgcolor() and style() directly return ANSI control codes for the given color or style value.

Using strip(), you can remove color and style codes from a string.

Example

<?php
require 'Console/Color.php';
//make it blue
print Console_Color::convert("%bHello World!%n\n");
//more colors
print Console_Color::convert("%rred%n, %ggreen%n, %yyellow%n\n");
?>
Console_Color (Previous) Tabular reference (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: tlhunter@renownedmedia.com
This will not work using the Windows Command Line. The control sequences will be printed to screen.

However, if you run this utility first, the colors will be readable by the command prompt:

http://adoxa.110mb.com/ansicon/index.html
Note by: tlhunter@renownedmedia.com
This does not work on Windows using the Command Prompt:
?[34mHello World!?[0m
Note by: moosh
http://moosh.et.son.brol.be/blog/index.php/2007/06/09/546-pear-console_color