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

本文介绍了如何在PHP中实现控制台输出彩色字体和制表符的方法,包括不同平台的效果,以及一个用于彩色输出的PHP类,并提供了使用示例。通过使用转义序列,可以设置文字颜色、背景颜色以及输出特效,例如光标的移动和清除屏幕。
摘要由CSDN通过智能技术生成

效果

b1bd2731dfa9a0ca1f08cd9a8367bd73.png

制表符

\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)脚本输出

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);

}

}

?>

颜色类的基本用法示例

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

0ee0dcfed8baab45de44224338456fd5.png

效果 win

1ad38cdf1cde3429dbac53ba301e05f1.png

打印所有前景和背景颜色

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值