我写了一个小命令行脚本来处理文档[带有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