PHP- 如何在終端輸出帶顏色的字型?
轉自: http://www.neatstudio.com/show-2568-1.shtml
終端顯示顏色,在以前的想法當中,都是因為有了.profile的配色方案。而我一般也都是 採用預設的(snakevil是寫過一個bash帶顏色的方案的。我覺得太花哨了就沒有使用)
為什麼突然間又想到這個?是因為在使用PHP輸出LOG的時候,千篇一率,從螢幕中找關鍵字很累,所以就想著,是不是用PHP也可以輸出這種帶顏色的關鍵字?當然,這是因為我正好看到了一個PHP是這麼輸出的,它就是laraval,它的工具(laraval.phar)在命令列的輸出就是有不同顏色的,它給了我指引,意思就是,這個想法是可以實現的。
OK。找資料,知道在終端中用指定的字元來做為背景色和字型色(http://blog.csdn.net/acmee/article/details/6613060)
文中這樣介紹:
XML/HTML程式碼- 一、shell下的實現方法
- 先來講在shell下,如何實現。用echo命令就可以實現,參看以下例子:
- echo -e "\033[32mHello, world!"
- 當你在終端裡敲下這條命令後,是不是發現系統用綠色輸出了"Hello,world!",不止如此,連之後的命令提示符都變成了綠色?不要著急,聽我繼續說。echo命令-e選項的作用是啟用終端對反斜線轉義符(即\)的解釋。引號內\033用於引導非常規字元序列,在這裡的作用就是引導設定輸出屬性,後邊的[32m就是將前景色設定為綠色,字母m表示設定的屬性類別,數字代表屬性值。設定可以單獨使用,例如:
- echo -e "\033[0m"
- 這行命令的作用是恢復屬性為預設值,也就是說0m設定項用於恢復預設值。現在你的終端是不是又一切正常了?
- 理解了這些,剩下的就簡單了。用這種命令,除了設定文字前景色,還可以設定很多屬性。下邊列出其他的設定項:
- --------------------------------------------------------------------------
- \033[0m 關閉所有屬性
- \033[1m 設定高亮度
- \033[4m 下劃線
- \033[5m 閃爍
- \033[7m 反顯
- \033[8m 消隱
- \033[30m 至 \33[37m 設定前景色
- \033[40m 至 \33[47m 設定背景色
- \033[nA 游標上移n行
- \033[nB 游標下移n行
- \033[nC 游標右移n行
- \033[nD 游標左移n行
- \033[y;xH設定游標位置
- \033[2J 清屏
- \033[K 清除從游標到行尾的內容
- \033[s 儲存游標位置
- \033[u 恢復游標位置
- \033[?25l 隱藏游標
- \033[?25h 顯示游標
- --------------------------------------------------------------------------
- 各數字所代表的顏色如下:
- 字背景顏色範圍:40----49
- 40:黑
- 41:深紅
- 42:綠
- 43:黃色
- 44:藍色
- 45:紫色
- 46:深綠
- 47:白色
- 字顏色:30-----------39
- 30:黑
- 31:紅
- 32:綠
- 33:黃
- 34:藍色
- 35:紫色
- 36:深綠
- 37:白色
- 另外,同類的多種設定項可以組合在一起,中間用分號(;)隔開。如下:
- echo -e "\033[20;1H\033[1;4;32mHello,world\033[0m"
- 這行命令首先\033[20;1H將游標移動到終端第20行第1列,之後的\033[1;4;32m將文字屬性設定為高亮、帶下劃線且顏色為綠色,然後輸出Hello,world;最後\033[0m將終端屬性恢復為預設值,這樣就不會看到連命令完成後的命令提示符也變了樣兒了。
- 通過以上各種命令的組合就可以實現對終端輸出地複雜控制。
- 二、如何在C程式設計中實現?
- 理解了以上在Shell中的實現方法,關於在C中如何實現就很簡單了。可以說只需要用printf函式代替上邊的echo -e就OK了。參見下例:
- int color = 32;
- printf("\033[20;1H\033[1;4;%dmHello, world.\033[0m", color);
- 這個例子類似上邊shell中最後那個例子,只是這裡顏色值通過變數color來指定(當然,也可以直接指定)。
- 三、聯想
- 看到這裡你可能會想,是不是在其他程式語言裡也可以用類似的方法實現對終端輸出的控制呢?答案是肯定的!比如在python中,可以如下輸出:
- color=32
- print “\033[20;1H\033[1;4;%dHello, world.\033[0m"%color
- 這個例子的效果跟上邊C的例子是相同的。
其實在看到這一段之前,snakevil在自己的github的專案(https://github.com/snakevil/bashrc.x)中也介紹過,其實我相對還是喜歡ubuntu的預設配色,snakevil的這個配色我是真心不是特別喜歡。。
但究竟怎麼用PHP輸出呢?在用PHP輸出之前,找了一下網路,發現已經有有用perl實現過了。那麼說實在的。如果沒有使用到一些特別的函式,其實php和perl實在是太象了,所以,可以直接參考(http://perldoc.perl.org/Term/ANSIColor.html),這裡的程式碼,除了那個類外,都還是可以復刻的。於是,再隨便找了點,果然還是有現成的PHP程式碼的(http://www.if-not-true-then-false.com/2010/php-class-for-coloring-php-command-line-cli-scripts-output-php-output-colorizing-using-bash-shell-colors/)
好吧。這個URL太長了。還是直接貼程式碼吧:
PHP程式碼- <?php
- class Colors {
- private $foreground_colors = array();
- private $background_colors = array();
- public function __construct() {
- // Set up shell colors
- $this->foreground_colors['black'] = '0;30';
- $this->foreground_colors['dark_gray'] = '1;30';
- $this->foreground_colors['blue'] = '0;34';
- $this->foreground_colors['light_blue'] = '1;34';
- $this->foreground_colors['green'] = '0;32';
- $this->foreground_colors['light_green'] = '1;32';
- $this->foreground_colors['cyan'] = '0;36';
- $this->foreground_colors['light_cyan'] = '1;36';
- $this->foreground_colors['red'] = '0;31';
- $this->foreground_colors['light_red'] = '1;31';
- $this->foreground_colors['purple'] = '0;35';
- $this->foreground_colors['light_purple'] = '1;35';
- $this->foreground_colors['brown'] = '0;33';
- $this->foreground_colors['yellow'] = '1;33';
- $this->foreground_colors['light_gray'] = '0;37';
- $this->foreground_colors['white'] = '1;37';
- $this->background_colors['black'] = '40';
- $this->background_colors['red'] = '41';
- $this->background_colors['green'] = '42';
- $this->background_colors['yellow'] = '43';
- $this->background_colors['blue'] = '44';
- $this->background_colors['magenta'] = '45';
- $this->background_colors['cyan'] = '46';
- $this->background_colors['light_gray'] = '47';
- }
- // Returns colored string
- public function getColoredString($string, $foreground_color = null, $background_color = null) {
- $colored_string = "";
- // Check if given foreground color found
- if (isset($this->foreground_colors[$foreground_color])) {
- $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
- }
- // Check if given background color found
- if (isset($this->background_colors[$background_color])) {
- $colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
- }
- // Add string and end coloring
- $colored_string .= $string . "\033[0m";
- return $colored_string;
- }
- // Returns all foreground color names
- public function getForegroundColors() {
- return array_keys($this->foreground_colors);
- }
- // Returns all background color names
- public function getBackgroundColors() {
- return array_keys($this->background_colors);
- }
- }
用法也比較簡單:
PHP程式碼- <?php
- // Create new Colors class
- $colors = new Colors();
- // Test some basic printing with Colors class
- echo $colors->getColoredString("Testing Colors class, this is purple string on yellow background.", "purple", "yellow") . "\n";
- echo $colors->getColoredString("Testing Colors class, this is blue string on light gray background.", "blue", "light_gray") . "\n";
- echo $colors->getColoredString("Testing Colors class, this is red string on black background.", "red", "black") . "\n";
- echo $colors->getColoredString("Testing Colors class, this is cyan string on green background.", "cyan", "green") . "\n";
- echo $colors->getColoredString("Testing Colors class, this is cyan string on default background.", "cyan") . "\n";
- echo $colors->getColoredString("Testing Colors class, this is default string on cyan background.", null, "cyan") . "\n";
當然,如果你覺得這個程式碼太麻煩,還有一個簡單的方法:
PHP程式碼- function colorize($text, $status) {
- $out = "";
- switch($status) {
- case "SUCCESS":
- $out = "[42m"; //Green background
- break;
- case "FAILURE":
- $out = "[41m"; //Red background
- break;
- case "WARNING":
- $out = "[43m"; //Yellow background
- break;
- case "NOTE":
- $out = "[44m"; //Blue background
- break;
- default:
- throw new Exception("Invalid status: " . $status);
- }
- return chr(27) . "$out" . "$text" . chr(27) . "[0m";
- }
- echo colorize("Your command was successfully executed...", "SUCCESS");
四種顏色也夠了。不過。。。NOTE的blue background。。。如果你的字元還是黑色的,真心看不到字串了。
至此,介紹完畢,你可以試試(我已經用在專案中了)