我如何语法突出显示PHP中exec()函数的输出?

问题描述 投票:0回答:2

我从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
2个回答
1
投票

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

aha是一个用C编写的Ansi到HTML适配器。它可以在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文件中非常有用。

1
投票

由于symfony,你也可以在纯PHP中完成它。使用ansi-to-html库。它没有依赖性,因此不需要使用symfony。

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

在那之后只是一个

require_once __DIR__.'/vendor/autoload.php';

use SensioLabs\AnsiConverter\AnsiToHtmlConverter;

$converter = new AnsiToHtmlConverter();

$html = $converter->convert($ansi);
© www.soinside.com 2019 - 2024. All rights reserved.