简体   繁体   中英

Output printed to terminal even with ob_start() on when script is executed from command line

I wrote a little command line script to process a document [a markdown file with lilypond musical notation inserts, just for completeness' sake].

#!/usr/bin/env php
<?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

<!DOCTYPE html>
<html lang="en">
<head>
  ...
</head>
<body>
  <div id="wrapper">
  <?php echo Markdown($body); ?>
  </div>
</body>
</html>

When I call:

$ ./phlily source.md

File gets generated properly, but the template contents are printed to console too:

GNU LilyPond 2.14.2
Processing `/Users/.../phlily/ly/4add05a74d249f34b3875ef6c3c1d79763927960.ly'
Parsing...
Converting to PNG...
<!DOCTYPE html>
<html lang="en">
<head>
...
</html>

and it's annoying because I want to read errors and warnings from the LilyPond script, being them buried behind the html wall in terminal.

Long story short, is it possible to shut the output buffer up in CLI environment?

I think you want ob_get_clean() instead of ob_get_flush() :

ob_get_clean :

ob_get_clean — Get current buffer contents and delete current output buffer

ob_get_flush :

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

In this case, "flush" means "send to stdout".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM