学习PHP-cli 模式在终端输出彩色标记文字以及动态内容
文字的各种效果标记写法
-
字体颜色与背景色
\033[30m 至 \33[37m 设置前景色 \033[40m 至 \33[47m 设置背景色 例如 echo "\033[30m this is a test msg \033[0m".PHP_EOL; echo "\033[45m this is a test msg \033[0m".PHP_EOL; 文字背景颜色范围: 40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色 文字颜色: 30:黑 31:红 32:绿 33:黄 34:蓝色 35:紫色 36:深绿 37:白色
登录后复制 标记闭合
所有效果在文本结尾处要加上闭合的标记:\033[0m;
登录后复制文字高亮等其他效果
\033[1m 文字高亮 \033[4m 下划线 \033[5m 闪烁 \033[7m 反显 \033[8m 消隐
登录后复制多种效果组合使用
多种效果组合使用时用英文分号隔开,例如蓝底红字下划线加闪烁 echo "\033[44;31;4m this is a test msg \033[0m".PHP_EOL;
登录后复制
光标的移动与设置
- 移动
\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 显示光标
登录后复制
简单的实现
文字效果操作类
namespace Console;class Style{ private $colors = [ "black"=>30, "red"=>31, "green"=>32, "yellow"=>33, "blue"=>34, "purple"=>35, "darkGreen"=>36, "white"=>37, ]; private $backgrounds = [ "black"=>40, "darkRed"=>41, "green"=>42, "yellow"=>43, "blue"=>44, "purple"=>45, "darkGreen"=>46, "white"=>47, ]; public $msg; public $style = []; public function __construct($msg){ $this->msg = $msg; } // 设置文本颜色 public function color( string $c ){ if( isset( $this->colors[$c]) ) $this->style[] = $this->colors[$c]; return $this; } // 设置背景色 public function bg( string $c ){ if(isset($this->backgrounds[$c]) ) $this->style[] = $this->backgrounds[$c]; return $this; } // 高亮 public function highLight(){ $this->style[] = 1; return $this; } // 下划线 public function underLine(){ $this->style[] = 4; return $this; } // 闪烁 public function twinkle(){ $this->style[] = 5; return $this; } // 反显 public function rshow(){ $this->style[] = 7; return $this; } // 消隐 public function hide(){ $this->style[] = 8; return $this; } public function toString(){ $this->style = array_unique($this->style); if($this->msg){ if(sizeof($this->style) ){ return "\033[".implode(';',$this->style)."m" . $this->msg . "\033[0m"; }else{ return $this->msg. "\033[0m"; } }else{ return false; } } }
登录后复制光标操作类
namespace Console; // 光标的信息以及操作 class Cursor{ // 光标设置 \033[y;xH private $x=0; private $y=0; // 获取光标X轴位置 public function offsetX(){ return $this->x; } // 获取光标Y轴位置 public function offsetY(){ return $this->y; } // 获取光标坐标 public function offset(){ return [ 'x'=>$this->x, 'y'=>$this->y, ]; } public function setX( int $x ){ $this->x = $x; } public function setY( int $y ){ $this->y = $y; } public function setOffset( int $x , int $y ){ $this->x = $x; $this->y = $y; return $this->toString(); } // 清屏 public function clear(){ return "\033[2J"; } public function show(){ return "\33[?25h"; } public function hide(){ return "\33[?25l"; } public function toString(){ if($this->x<0)$dx = 'D'; else $dx = 'C'; if($this->y<0)$dy = 'A'; else $dy = 'B'; $absx = abs($this->x); $absy = abs($this->y); return "\033[{$absx}{$dx}\033[{$absy}{$dy}"; } }
登录后复制table类,通便html的table标记语言,输出table
namespace Console;class Table{ public $table=[]; private $getV; private $currentTag='table'; private $props = [ 'color','bg','twinkle','highLight','underLine','colspan','rowspan','width','border','align' ]; public function __construct( string $html){ // 解析字符串最好 $this->html=$html; $this->parse(); } // 解析字符串 将table的每个tr td以及属性都解析出来 private function parse(){ if( !preg_match("/<table(\s+.*?)?>(.*?)<\/table>/is",$this->html) || !preg_match("/<tr(\s+.*?)?>(.*?)<\/tr>/is",$this->html) || !preg_match("/<td(\s+.*?)?>(.*?)<\/td>/is",$this->html) ){ die('标签有误,必须包含table tr td标签且标签闭合'); } $this->table['html'] = $this->html; $this->getPrototype('table',$this->table); preg_match_all("/<table(\s+.*?)?>(.*?)<\/table>/is",$this->html,$innerTable); if( $innerTable[0][0] ){ preg_match_all("/<tr(\s+.*?)?>(.*?)<\/tr>/is",$this->html,$trList); if( $trList[0] ){ $this->table['tr'] = $trList[0]; foreach($this->table['tr'] as $k=>$tr){ $this->table['tr'][$k] = []; preg_match_all("/<td(\s+.*?)?>(.*?)<\/td>/is",$tr,$tdList); $this->table['tr'][$k]['td'] = $tdList[0]; $this->table['tr'][$k]['html'] =$tr; $this->getPrototype('tr',$this->table['tr'][$k]); foreach ($this->table['tr'][$k]['td'] as $kk=>$td){ $this->table['tr'][$k]['td'][$kk] = [ 'html'=>$td, 'content'=>$tdList[2][$kk] ]; $this->getPrototype('td',$this->table['tr'][$k]['td'][$kk]); } } }else{ die('Tr 不存在'); } }else{ die('Table 不存在'); } } private function getPrototype(string $tag,&$v){ preg_match("/<{$tag}(\s.*?)?>(.*?)<\/{$tag}>/is",$v['html'],$arr); if(strlen($arr[1])){ $arr2 = explode(" ", trim(preg_replace("/( +)/is"," ",$arr[1]))); foreach ($arr2 as $arr3){ $arr4 = explode('=',str_replace([' ',"\"","\'"],'',$arr3)); if(in_array($arr4[0],$this->props)){ $v[$arr4[0]] = $arr4[1]; } } } } }
登录后复制console类,输出自定义的文本或table格式文本
namespace Console;class Console{ public $cursor; private $msgList=[]; private $lastCountLine=0; public function __construct(){ $this->cursor= new Cursor(); } private static function getStrlen($str){ if(!$str) return 0; $l=0; $strArr = preg_split('//u',$str,-1, PREG_SPLIT_NO_EMPTY); if(is_array($strArr)){ foreach($strArr as $v){ if(preg_match("/^[\x{4e00}-\x{9fa5}]$/u", $v)) $l += 2; else $l += 1; } } return $l; } public function write($msg){ $msgStyle = new Style($msg); $this->msgList[] = $msgStyle; return $msgStyle; } public function table(array $table){ foreach($table['tr'] as $tr){ foreach($tr['td'] as $td){ if($td['content']){ $len = self::getStrlen($td['content']); // 显示问题矫正 $tdlen = $td['width'] ?? max(15,$len); $tdlen = max($tdlen,$len); $align = $td['align'] ??$tr['align']??$table['align']?? false; if( $tdlen>$len ){ if( $align=='left'){ $td['content'] = $td['content'].str_repeat(' ',$tdlen-$len); }else if($align=='right'){ $td['content'] = str_repeat(' ',$tdlen-$len) . $td['content']; }else{ $td['content'] = str_repeat(' ',floor(($tdlen-$len)/2)) . $td['content'] . str_repeat(' ',ceil(($tdlen-$len)/2)); } } $msg = $this->write($td['content']); $color = $td['color'] ?? $tr['color'] ??$table['color']??false; $twinkle = $td['twinkle'] ?? $tr['twinkle'] ??$table['twinkle']??false; $bg = $td['bg '] ?? $tr['bg '] ??$table['bg ']??false; $highLight = $td['highLight'] ?? $tr['highLight'] ??$table['highLight']??false; $underLine = $td['underLine'] ?? $tr['underLine'] ??$table['underLine']??false; if($color) $msg->color($color); if($bg) $msg->bg($bg); if($twinkle) $msg->twinkle(); if($highLight) $msg->highLight(); if($underLine) $msg->underLine(); } } $this->write("\n"); } $this->dump(); } public function dump(){ foreach( $this->msgList as $msg){ echo $msg->toString(); } $this->lastCountLine = $this->getLine(); $this->msgList=[]; } public function cursorMove( int $x , int $y ) { $this->write( $this->cursor->setOffset( $x,$y)); } public function getCountLine(){ return $this->lastCountLine; } private function getLine(){ if(!sizeof($this->msgList)) return 0; else{ $line=0; foreach( $this->msgList as $msg ){ for($i=0;$i<mb_strlen($msg->msg);$i++){ if(mb_substr($msg->msg ,$i,1) == PHP_EOL) $line++; } } return $line; } } }
登录后复制
实例
- 直接输出带效果的文字截图:
// 实例化console类$c = new Console\Console();// 向console类里添加文本$msg = $c->write('this is a test msg.'.PHP_EOL);// 文本设置效果$msg->color('red')->bg('darkGreen')->highLight()->underLine();// 再次添加一个文本$msg2 = $c->write('this is another msg.'.PHP_EOL);// 文本设置效果$msg2->color('yellow')->bg('purple')->twinkle()->underLine();// 输出文本$c->dump();
登录后复制 - 通过table标签标记输出文本截图:
/* table标记注意事项 1. 标签有且有table、tr、td,且表桥闭合 2. table、tr、td标签属性可设置color、bg、twinkle(等文字效果)、width、align。目前只实现了这些 3. 数据的动态展示原理是通过计算table的行数并移动光标达到的,并不是很严谨,效果也一般 */ // 用于随机字符 $zmstr='abcdefghijklmnopqrstuvwxyz'; while(true){ $html=' <table align="right"> <tr color="red"> <td width=20 >英文</td> <td width=30>数字</td> <td width=30>中文</td> </tr> <tr> </tr> '; for($i=0;$i<5;$i++){ $num = rand(10,99); $color=''; if($num>80){ $color='red'; }else if($num>50){ $color='green'; }else if($num>30){ $color='purple'; } $html.='<tr> <td width=20>'.$zmstr[rand(0,25)].$zmstr[rand(0,25)].$zmstr[rand(0,25)].$zmstr[rand(0,25)].'</td> <td width=30 color="'.$color.'">'.$num.'</td> <td width=30 >测试</td> </tr>'; } $html.='</table>'; // 移动光标 $c->cursorMove(-1000,-($c->getCountLine())); // 通过table标签实例Table类 $t = new Table($html); // 输出解析后的table标签 $c->table($t->table); sleep(2); }
登录后复制
相关学习推荐:PHP编程从入门到精通
以上是学习PHP-cli 模式在终端输出彩色标记文字以及动态内容的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。
