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
#!/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
相关文章推荐
- linux下利用echo命令输出带颜色以及带字体格式的字符串
- 【程序】给C++的cout和fstream添加Unicode支持,使其能向屏幕或文件输入/输出wchar_t字符串
- PHP 输出中文 JSON 字符串
- Win32控制台 cout输出带颜色的字符串
- 搭建MVC框架,实现在地址栏传递参数index.php?c=news&a=add后,输出"这里是新闻添加"
- 在字符串指定的索引下添加字符,输出换行
- php如何输出中文json格式字符串
- PHP中字符串分割、获取子串以及向文本框中添加值
- 将多维php数组转换为XML文件或以字符串输出
- PHP 图片上传,添加字符串水印,添加图片(指定四个角) 图片压缩
- PHP笔记=>var_dump,大小写,调试输出深度,字符串 this和self
- PHP水印类,支持添加图片、文字、填充颜色区域的实现
- php 输出带变量字符串(echo 函数的应用)
- PHP中字符串分割、获取子串以及向文本框中添加值
- php输出字体颜色时遇到的小问题
- var_export()函数的使用举例(后续添加其他的php输出函数)
- PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
- PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
- php 字符串中任意添加
- 给大家分享一个PHP控制字符串输出函数案例吧