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

废话少说,直接上代码:

 

#!/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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值