最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

PHP 或其他脚本 在终端输出带颜色的字体 或制表符

LINX ajiang-tuzi 1582浏览

效果

ddhe

制表符

\r\n 换行
\n 换行 linux
\t 跳格
注意
\t到底跳过几个空格是没有规定,也没有标准的,每个输出设备会规定自己设备上\t会定位到某个整数单位倍数处,
比如有的设备规定\t定位到8字符整数倍数处,
假定某个字符串在制表符\t的前面有n个字符,那么\t将跳过8-n%8个空格。

垂直制表符:\v (只对打印设备有效,显示器无效)

颜色控制 支持win 下PS 终端

 echo "\033[0;31m Hi Tecmint \x1B[0m";
#printf("\033[1;33m Hello World. \033[0m \n");  
#颜色如下:  
none         = "\033[0m"  
black        = "\033[0;30m"  
dark_gray    = "\033[1;30m"  
blue         = "\033[0;34m"  
light_blue   = "\033[1;34m"  
green        = "\033[0;32m"  
light_green -= "\033[1;32m"  
cyan         = "\033[0;36m"  
light_cyan   = "\033[1;36m"  
red          = "\033[0;31m"  
light_red    = "\033[1;31m"  
purple       = "\033[0;35m"  
light_purple = "\033[1;35m"  
brown        = "\033[0;33m"  
yellow       = "\033[1;33m"  
light_gray   = "\033[0;37m"  
white        = "\033[1;37m"  
  
字背景颜色范围: 40--49                   字颜色: 30--39  
            40: 黑                          30: 黑  
        41:红                          31: 红  
        42:绿                          32: 绿  
        43:黄                          33: 黄  
        44:蓝                          34: 蓝  
        45:紫                          35: 紫  
        46:深绿                        36: 深绿  
        47:白色                        37: 白色  
  
输出特效格式控制:    
\033[0m  关闭所有属性    
\033[1m   设置高亮度    
\03[4m   下划线    
\033[5m   闪烁    
\033[7m   反显    
\033[8m   消隐    
\033[30m   --   \033[37m   设置前景色    
\033[40m   --   \033[47m   设置背景色  
  
光标位置等的格式控制:   
\033[nA  光标上移n行    
\03[nB   光标下移n行    
\033[nC   光标右移n行    
\033[nD   光标左移n行    
\033[y;xH设置光标位置    
\033[2J   清屏    
\033[K   清除从光标到行尾的内容    
\033[s   保存光标位置    
\033[u   恢复光标位置    
\033[?25l   隐藏光标    
\33[?25h   显示光标  

一些控制脚本

PHP类用于着色PHP命令行(CLI)脚本输出

<?php
namespace Weicot;
	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

	// 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";

?>

效果 Linux
_zmhwceeqvqlgrkdh7
效果 win

打印所有前景和背景颜色

// Create new Colors class
		$colors = new Colors();

		// Get Foreground Colors
		$fgs = $colors->getForegroundColors();
		// Get Background Colors
		$bgs = $colors->getBackgroundColors();

		// Loop through all foreground and background colors
		$count = count($fgs);
		for ($i = 0; $i < $count; $i++) {
			echo $colors->getColoredString("Test Foreground colors", $fgs[$i]) . "\t";
			if (isset($bgs[$i])) {
				echo $colors->getColoredString("Test Background colors", null, $bgs[$i]);
			}
			echo "\n";
		}
		echo "\n";

		// Loop through all foreground and background colors
		foreach ($fgs as $fg) {
			foreach ($bgs as $bg) {
				echo $colors->getColoredString("Test Colors", $fg, $bg) . "\t";
			}
			echo "\n";
		}

效果
weicot-color
参考
https://en.wikipedia.org/wiki/ANSI_escape_code

转载请注明:(●--●) Hello.My Weicot » PHP 或其他脚本 在终端输出带颜色的字体 或制表符