php 命令行 打印,php – 当从命令行执行脚本时,即使使用ob_start(),输出也会打印到终端...

我写了一个小命令行脚本来处理文档[带有lilypond乐谱插入的降价文件,只是为了完整性].

#!/usr/bin/env php

$body = "";

...

// text gets processed here and stored in $body

...

ob_start();

include 'template.php';

file_put_contents(

__DIR__ . '/' . str_replace('.md', '.html', $argv[1]),

ob_get_flush()

);

的template.php

...

我打电话的时候:

$./phlily source.md

文件生成正确,但模板内容也打印到控制台:

GNU LilyPond 2.14.2

Processing `/Users/.../phlily/ly/4add05a74d249f34b3875ef6c3c1d79763927960.ly'

Parsing...

Converting to PNG...

...

这很烦人,因为我想从LilyPond脚本中读取错误和警告,将它们隐藏在终端的html墙后面.

简而言之,是否可以在CLI环境中关闭输出缓冲区?

解决方法:

我想你想要ob_get_clean()而不是ob_get_flush():

ob_get_clean — Get current buffer contents and delete current output buffer

ob_get_flush — Flush the output buffer, return it as a string and turn off output buffering

在这种情况下,“flush”表示“发送到stdout”.

file_put_contents(

__DIR__ . '/' . str_replace('.md', '.html', $argv[1]),

ob_get_clean()

);

标签:output-buffering,php,command-line-interface

来源: https://codeday.me/bug/20190729/1571601.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值