PHP 给输出的字符串添加颜色
来源:互联网 发布:linux监听端口命令 编辑:程序博客网 时间:2024/06/10 20:57
废话少说,直接上代码:
#!/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
- PHP 给输出的字符串添加颜色
- 利用case语句给输出的字符串加颜色
- 给Ubuntu下的Git添加颜色
- Unity3D: 给字符串中的部分字体添加颜色突出显示
- 给menu添加颜色
- 给脚本添加颜色
- 给颜色添加透明度
- 给颜色添加透明度
- linux下printf输出字符串的颜色
- Linux下printf输出字符串的颜色
- Linux下 printf 输出字符串的颜色
- Linux下printf输出字符串的颜色
- erlang的终端带颜色输出与中文字符串输出
- 给php代码添加规范的注释
- 关于给对话框添加颜色
- 给Div添加边框颜色
- 给点云添加颜色
- 给eclipse添加颜色主题,保护你的眼睛
- host文件工作原理
- 怎样在除了Ondraw或者OnPaint函数中画图?
- java操作pdf
- Struts 各个版本的源代码
- KMP算法和实现
- PHP 给输出的字符串添加颜色
- VC 工具打不开 ClassView
- Array或ArrayCollection作为java方法参数
- 两个小问题
- 《程序员》百期文章回顾之9——中国优秀软件架构师感悟录
- 关于消息和消息队列
- 为应用程序制作帮助文件
- 《杜拉拉升职记》《四个丘比特》
- strftime函数将时间格式化