doumei4964 2019-07-31 09:37
浏览 52


In my PHP script I'm executing a bash script with popen() and I output the response. But the response has a different encoding, and I don't know how to convert it.

I tried to convert it to utf8 but no results. Also tried to set the locale to nl_NL.utf8.

PHP function:

while (@ ob_end_flush()); // end all output buffers if any

$proc = popen("$cmd 2>&1 ; echo Exit status : $?", 'r');

$live_output     = "";
$complete_output = "";

while (!feof($proc))
    $live_output     = fread($proc, 4096);
    $complete_output = $complete_output . $live_output;
    echo "$live_output";
    @ flush();


Example response:

[2K[1A[2K[G⠋ load plugins
[2K[1A[2K[Gsuccess load plugins - 0.975 s
⠋ load plugins

This should be just:

success load plugins - 0.975 s

The ⠋ thing is a loader which is moving. Could that be the problem? Or are these characters the colorcode of the line?

  • 写回答

1条回答 默认 最新

  • dpthuyh1678 2019-08-01 14:26

    Those are ANSI Escape Sequences, they are not bash commands but console commands used for coloring output or replacing/updating output on a single line.

    Often commands offer a —no-color flag.

    Also, a environment variable exists for common commands: NO_COLOR. Here's a list of common flags/variables to disable colored output =>

    Otherwise, you could always filter the output with a regex: /(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用