【问题标题】:How can I syntax highlight the output from the exec() function in PHP?如何语法高亮 PHP 中 exec() 函数的输出?
【发布时间】:2014-09-18 19:43:00
【问题描述】:

我正在从exec() function 获取输出,我希望在结果中突出显示一些语法。

原始输出

当前输出是原始的:

* [35mmanu[m/etc/init.d/mast: line 105: /var/log/mast/mast-all.log: Permission denied 

[35m 等文字是颜色highlighting in shell context

目标

我想用 HTML 来做,我已经有了一个彩色的 shell 脚本。

【问题讨论】:

标签: php shell syntaxhighlighter passthru


【解决方案1】:

A library to convert ANSI escapes (terminal formatting/color codes) to HTML这个问题有答案。

解决方案

aha 是一个 AnsiHTML 的适配器,用 C 编写。它在 Ubuntu 中可用 包和 github theZiz/aha

我的代码很简单:

   exec("$command | aha", $output, $exitCode);
   foreach($output as $k => $line) {
       if ($line == '1') { continue; }
       echo "$line";
   }

说明

   aha takes SGR-colored Input and prints W3C conform HTML-Code.
   aha reads the Input from a file or stdin and writes HTML-Code to stdout.

有一些nice options

  • --black-b:黑色背景和白色“标准颜色”
  • --word-wrap-w:在 html 文件中换行。这适用于支持 CSS3 的浏览器以及许多较旧的浏览器。
  • --no-header-n:不要在生成的 HTML 中包含标题,这对于包含在完整的 HTML 文件中很有用。

【讨论】:

    【解决方案2】:

    感谢 symfony,您也可以在纯 PHP 中执行此操作。使用ansi-to-html 库。 它没有依赖关系,因此不需要使用 symfony。

    您可以通过在源文件夹中处理 SensioLabs\AnsiConverter\AnsiToHtmlConverte.php 或使用 composer 手动安装它 作曲家需要 sensiolabs/ansi-to-html

    之后就是一个

    require_once __DIR__.'/vendor/autoload.php';
    
    use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
    
    $converter = new AnsiToHtmlConverter();
    
    $html = $converter->convert($ansi);
    

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多