{"id":674,"date":"2021-11-17T15:00:00","date_gmt":"2021-11-17T15:00:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=674"},"modified":"2021-11-17T15:00:00","modified_gmt":"2021-11-17T15:00:00","slug":"674","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/11\/17\/674\/","title":{"rendered":"PHP \u547d\u4ee4\u884c(CLI)\u811a\u672c\u4f7f\u7528\u5f69\u8272\u7740\u8272\u7c7b: PHP Class for Coloring PHP Command Line (CLI) Scripts Output"},"content":{"rendered":"<p><a href=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/3775419271.png\"><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/3775419271.png\" alt=\"11.PNG\" title=\"11.PNG\"><\/a><\/p>\n<h2>PHP \u547d\u4ee4\u884c(CLI)\u811a\u672c\u4f7f\u7528\u5f69\u8272\u7740\u8272\u7c7b PHP Class for Coloring PHP Command Line (CLI) Scripts Output<\/h2>\n<ul>\n<li><a href=\"https:\/\/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\/\">\u539f\u6587\u94fe\u63a5<\/a><\/li>\n<\/ul>\n<p>PHP \u547d\u4ee4\u884c\u754c\u9762 (CLI) \u6ca1\u6709\u5185\u7f6e\u7684\u811a\u672c\u8f93\u51fa\u7740\u8272\u3002 \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u7c7b\u5728 PHP CLI \u8f93\u51fa\u4e0a\u6dfb\u52a0\u989c\u8272\u3002<br \/>\u6b64\u7c7b\u4ec5\u9002\u7528\u4e8e <code>Bash shell<\/code>\u3002 \u8fd9\u4e2a\u7c7b\u5f88\u5bb9\u6613\u4f7f\u7528\uff0c\u53ea\u9700\u521b\u5efa\u7c7b\u7684\u65b0\u5b9e\u4f8b\uff0c\u8c03\u7528 <code>getColoredString<\/code> \u51fd\u6570,<br \/>\u5e76\u4f7f\u7528\u5b57\u7b26\u4e32\u548c\u524d\u666f\u8272\u6216\u80cc\u666f\u8272\u505a\u53c2\u6570\u3002<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/hongwenjun\/srgb\/blob\/master\/php\/colors_class.php\">colors_class.php<\/a><\/p>\n<pre><code class=\"lang-php\">&lt;?php\nclass Colors\n{\n  private $foreground_colors = array();\n  private $background_colors = array();\n\n  public function __construct()\n  {\n      \/\/ Set up shell colors\n      $this-&gt;foreground_colors['black'] = '0;30';\n      $this-&gt;foreground_colors['dark_gray'] = '1;30';\n      $this-&gt;foreground_colors['blue'] = '0;34';\n      $this-&gt;foreground_colors['light_blue'] = '1;34';\n      $this-&gt;foreground_colors['green'] = '0;32';\n      $this-&gt;foreground_colors['light_green'] = '1;32';\n      $this-&gt;foreground_colors['cyan'] = '0;36';\n      $this-&gt;foreground_colors['light_cyan'] = '1;36';\n      $this-&gt;foreground_colors['red'] = '0;31';\n      $this-&gt;foreground_colors['light_red'] = '1;31';\n      $this-&gt;foreground_colors['purple'] = '0;35';\n      $this-&gt;foreground_colors['light_purple'] = '1;35';\n      $this-&gt;foreground_colors['brown'] = '0;33';\n      $this-&gt;foreground_colors['yellow'] = '1;33';\n      $this-&gt;foreground_colors['light_gray'] = '0;37';\n      $this-&gt;foreground_colors['white'] = '1;37';\n\n      $this-&gt;background_colors['black'] = '40';\n      $this-&gt;background_colors['red'] = '41';\n      $this-&gt;background_colors['green'] = '42';\n      $this-&gt;background_colors['yellow'] = '43';\n      $this-&gt;background_colors['blue'] = '44';\n      $this-&gt;background_colors['magenta'] = '45';\n      $this-&gt;background_colors['cyan'] = '46';\n      $this-&gt;background_colors['light_gray'] = '47';\n  }\n\n  \/\/ Returns colored string\n  public function getColoredString($string, $foreground_color = null, $background_color = null)\n  {\n      $colored_string = \"\";\n\n      \/\/ Check if given foreground color found\n      if (isset($this-&gt;foreground_colors[$foreground_color])) {\n          $colored_string .= \"\u000033[\" . $this-&gt;foreground_colors[$foreground_color] . \"m\";\n      }\n      \/\/ Check if given background color found\n      if (isset($this-&gt;background_colors[$background_color])) {\n          $colored_string .= \"\u000033[\" . $this-&gt;background_colors[$background_color] . \"m\";\n      }\n\n      \/\/ Add string and end coloring\n      $colored_string .=  $string . \"\u000033[0m\";\n\n      return $colored_string;\n  }\n\n  \/\/ Returns all foreground color names\n  public function getForegroundColors()\n  {\n      return array_keys($this-&gt;foreground_colors);\n  }\n\n  \/\/ Returns all background color names\n  public function getBackgroundColors()\n  {\n      return array_keys($this-&gt;background_colors);\n  }\n}\n?&gt;<\/code><\/pre>\n<h2>\u989c\u8272\u7c7b\u57fa\u672c\u4f7f\u7528\u793a\u4f8b: Colors class basic usage examples<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/1745788130.png\" alt=\"1.png\" title=\"1.png\"><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/hongwenjun\/srgb\/blob\/master\/php\/colors.php\">colors.php<\/a><\/p>\n<pre><code class=\"lang-php\">&lt;?php\n  include(\"colors_class.php\");\n  \/\/ Create new Colors class\n  $colors = new Colors();\n\n  \/\/ Test some basic printing with Colors class\n  echo $colors-&gt;getColoredString(\"Testing Colors class, this is purple string on yellow background.\", \"purple\", \"yellow\") . \"\n\";\n  echo $colors-&gt;getColoredString(\"Testing Colors class, this is blue string on light gray background.\", \"blue\", \"light_gray\") . \"\n\";\n  echo $colors-&gt;getColoredString(\"Testing Colors class, this is red string on black background.\", \"red\", \"black\") . \"\n\";\n  echo $colors-&gt;getColoredString(\"Testing Colors class, this is cyan string on green background.\", \"cyan\", \"green\") . \"\n\";\n  echo $colors-&gt;getColoredString(\"Testing Colors class, this is cyan string on default background.\", \"cyan\") . \"\n\";\n  echo $colors-&gt;getColoredString(\"Testing Colors class, this is default string on cyan background.\", null, \"cyan\") . \"\n\";\n?&gt;<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>\u6253\u5370\u6240\u6709\u524d\u666f\u8272\u548c\u80cc\u666f\u8272: All Foreground and background colors printed<\/h2>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/hongwenjun\/srgb\/blob\/master\/php\/all_colors.php\">all_colors.php<\/a><\/p>\n<pre><code class=\"lang-php\">&lt;?php\n  include(\"colors_class.php\");\n  \/\/ Create new Colors class\n  $colors = new Colors();\n\n  \/\/ Get Foreground Colors\n  $fgs = $colors-&gt;getForegroundColors();\n  \/\/ Get Background Colors\n  $bgs = $colors-&gt;getBackgroundColors();\n\n  \/\/ Loop through all foreground and background colors\n  $count = count($fgs);\n  for ($i = 0; $i &lt; $count; $i++) {\n      echo $colors-&gt;getColoredString(\"Test Foreground colors\", $fgs[$i]) . \"    \";\n      if (isset($bgs[$i])) {\n          echo $colors-&gt;getColoredString(\"Test Background colors\", null, $bgs[$i]);\n      }\n      echo \"\n\";\n  }\n  echo \"\n\";\n\n  \/\/ Loop through all foreground and background colors\n  foreach ($fgs as $fg) {\n      foreach ($bgs as $bg) {\n          echo $colors-&gt;getColoredString(\"Test Colors\", $fg, $bg) . \"    \";\n      }\n      echo \"\n\";\n  }\n?&gt;<\/code><\/pre>\n<h2>\u5f53\u7136\u8fd8\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4ee3\u7801\u5f88\u5c0f\uff0c\u56db\u79cd\u989c\u8272\u4e5f\u591f\u7528\u4e86<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/287314812.png\" alt=\"3.png\" title=\"3.png\"><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/hongwenjun\/srgb\/blob\/master\/php\/colorize.php\">colorize.php<\/a><\/p>\n<pre><code>&lt;?php\nfunction colorize($text, $status) {\n $out = \"\";\n switch($status) {\ncase \"SUCCESS\":\n $out = \"[42m\"; \/\/Green background\n break;\ncase \"FAILURE\":\n $out = \"[41m\"; \/\/Red background\n break;\ncase \"WARNING\":\n $out = \"[43m\"; \/\/Yellow background\n break;\ncase \"NOTE\":\n $out = \"[44m\"; \/\/Blue background\n break;\ndefault:\n throw new Exception(\"Invalid status: \" . $status);\n }\n return chr(27) . \"$out\" . \"$text\" . chr(27) . \"[0m\";\n}\n\necho colorize(\"Your command was successfully executed...\n\", \"SUCCESS\");\necho colorize(\"Your command was FAILURE executed...\n\", \"FAILURE\");\necho colorize(\"Your command was WARNING executed...\n\", \"WARNING\");\necho colorize(\"Your command was NOTE    info...\n\", \"NOTE\");\n\n?&gt;<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u547d\u4ee4\u884c(CLI)\u811a\u672c\u4f7f\u7528\u5f69\u8272\u7740\u8272\u7c7b PHP Class for Coloring PHP C [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[34],"class_list":["post-674","post","type-post","status-publish","format-standard","hentry","category-learn","tag-php"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}