您的位置:首页 > 编程语言 > PHP开发

PHP 给输出的字符串添加颜色

2010-04-29 14:56 169 查看
废话少说,直接上代码:

#!/usr/bin/php
<?php
class color
{
var $consoleCodes = array();
var $convertTable = array();

function color()
{
$this->init();
}

function getConsoleCodes($define)
{
$codes = array();
$fgcolor = '';
$style = '';
$bgcolor = '';
extract($define);

if($fgcolor == 'reset') return "/033[0m";
if(isset($this->consoleCodes['fgcolor'][$fgcolor]))
$codes[] = $this->consoleCodes['fgcolor'][$fgcolor];
if(isset($this->consoleCodes['style'][$style]))
$codes[] = $this->consoleCodes['style'][$style];
if(isset($this->consoleCodes['bgcolor'][$bgcolor]))
$codes[] = $this->consoleCodes['bgcolor'][$bgcolor];

$codes = implode(';', $codes);
return "/033[{$codes}m";
}

function convert($string, $mode = 'replace')
{
foreach($this->convertTable as $controlChar => $controlDefine)
{
if(strpos($string, $controlChar) !== false)
{
if($mode == 'replace')
{
$string = str_replace($controlChar, $this->getConsoleCodes($controlDefine), $string);
}
elseif($mode == 'strip')
{
$string = str_replace($controlChar, '', $string);
}
}
}
return $string;
}

function init()
{
$this->consoleCodes['fgcolor']['black'] = 30;
$this->consoleCodes['fgcolor']['red'] = 31;
$this->consoleCodes['fgcolor']['green'] = 32;
$this->consoleCodes['fgcolor']['brown'] = 33;
$this->consoleCodes['fgcolor']['blue'] = 34;
$this->consoleCodes['fgcolor']['purple'] = 35;
$this->consoleCodes['fgcolor']['cyan'] = 36;
$this->consoleCodes['fgcolor']['grey'] = 37;
$this->consoleCodes['fgcolor']['yellow'] = 33;

$this->consoleCodes['style']['normal'] = 0;
$this->consoleCodes['style']['bold'] = 1;
$this->consoleCodes['style']['light'] = 1;
$this->consoleCodes['style']['underscore'] = 4;
$this->consoleCodes['style']['underline'] = 4;
$this->consoleCodes['style']['blink'] = 5;
$this->consoleCodes['style']['inverse'] = 6;
$this->consoleCodes['style']['hidden'] = 8;
$this->consoleCodes['style']['concealed'] = 8;

$this->consoleCodes['bgcolor']['black'] = 40;
$this->consoleCodes['bgcolor']['red'] = 41;
$this->consoleCodes['bgcolor']['green'] = 42;
$this->consoleCodes['bgcolor']['brown'] = 43;
$this->consoleCodes['bgcolor']['yellow'] = 43;
$this->consoleCodes['bgcolor']['blue'] = 44;
$this->consoleCodes['bgcolor']['purple'] = 45;
$this->consoleCodes['bgcolor']['cyan'] = 46;
$this->consoleCodes['bgcolor']['grey'] = 47;

$this->convertTable['@y'] = array('fgcolor' => 'yellow');
$this->convertTable['@g'] = array('fgcolor' => 'green' );
$this->convertTable['@b'] = array('fgcolor' => 'blue' );
$this->convertTable['@r'] = array('fgcolor' => 'red' );
$this->convertTable['@p'] = array('fgcolor' => 'purple');
$this->convertTable['@m'] = array('fgcolor' => 'purple');
$this->convertTable['@c'] = array('fgcolor' => 'cyan' );
$this->convertTable['@w'] = array('fgcolor' => 'grey' );
$this->convertTable['@k'] = array('fgcolor' => 'black' );
$this->convertTable['@n'] = array('fgcolor' => 'reset' );
$this->convertTable['@Y'] = array('fgcolor' => 'yellow', 'style' => 'light');
$this->convertTable['@G'] = array('fgcolor' => 'green', 'style' => 'light');
$this->convertTable['@B'] = array('fgcolor' => 'blue', 'style' => 'light');
$this->convertTable['@R'] = array('fgcolor' => 'red', 'style' => 'light');
$this->convertTable['@P'] = array('fgcolor' => 'purple', 'style' => 'light');
$this->convertTable['@M'] = array('fgcolor' => 'purple', 'style' => 'light');
$this->convertTable['@C'] = array('fgcolor' => 'cyan', 'style' => 'light');
$this->convertTable['@W'] = array('fgcolor' => 'grey', 'style' => 'light');
$this->convertTable['@K'] = array('fgcolor' => 'black', 'style' => 'light');
$this->convertTable['@N'] = array('fgcolor' => 'reset', 'style' => 'light');
$this->convertTable['@3'] = array('bgcolor' => 'yellow');
$this->convertTable['@2'] = array('bgcolor' => 'green' );
$this->convertTable['@4'] = array('bgcolor' => 'blue' );
$this->convertTable['@1'] = array('bgcolor' => 'red' );
$this->convertTable['@5'] = array('bgcolor' => 'purple');
$this->convertTable['@6'] = array('bgcolor' => 'cyan' );
$this->convertTable['@7'] = array('bgcolor' => 'grey' );
$this->convertTable['@0'] = array('bgcolor' => 'black' );
$this->convertTable['@F'] = array('style' => 'blink');
$this->convertTable['@U'] = array('style' => 'underline');
$this->convertTable['@8'] = array('style' => 'inverse');
$this->convertTable['@9'] = array('style' => 'bold');
$this->convertTable['@_'] = array('style' => 'bold');
}
}

$color_obj = new color();
echo $color_obj->convert( "@gHello@n/n" );
echo $color_obj->convert( "@rHello@n/n" );
echo $color_obj->convert( "@F@rHello@n/n" );

?>

注:/033 same as ctrl + [ in php (^[ whose ascii code is 0x33)
^[[32mHello => Hello with green
^[[32m^[[5mHello => Hello with blink green
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: