note 99077 deleted from function.chr by danbrown
| From: | danbrown@php.net | Date: | Tue, 27 Jul 2010 01:19:23 +0000 |
| Subject: | note 99077 deleted from function.chr by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171149@lists.php.net to get a copy of this message | ||
Note Submitter: alex dot chesser at gmail dot com
----
Thanks to Glen Cooper for the initial function. I've modified and tweaked to get the structure
of being able to specify background and foreground colors.
<?php
function ec2($text, $fg = null, $bg = null, $d = 'normal') {
$colors = array( 'black' => 0,
'red' => 1,
'green' => 2,
'yellow' => 3,
'blue' => 4,
'magenta' => 5,
'cyan' => 6,
'white' => 7);
$decoration = array ( 'normal' => 0,
'bold' => 1,
'underline' => 4,
'reverse' => 7);
$fg = $fg ? "3{$colors[$fg]}m" : "32m";
$bg = $bg ? "4{$colors[$bg]};" : null;
$d = "[{$decoration[$d]};";
echo chr(27)."$d$bg$fg"."$text".chr(27)."[0m";
}
$text = "Here is some text\n";
ec2($text, 'black', 'white');
ec2($text, 'blue', 'white', 'underline');
ec2($text, 'green', null, 'bold');
?>
I've based it on the definition found here:
http://en.wikipedia.org/wiki/ANSI_escape_code
I'm sure there is a lot of further improvement that can be done... but it's a step.