How to prevent an included file to generate output in PHP? - php

I've got a software that generates outputs using a pattern for several records of data. This file is generated in a specific interval, and due the software is closed source I cant change it.
My Pattern is something like (Without a trailing CR or LF):
<? $elem[]='%putValueOfRecordHere%' ?>
The output will be:
<? $elem[]='1' ?>
<? $elem[]='2' ?>
<? $elem[]='3' ?>
In fact the software adds a CRLF for each record, including and using this file would add a lot of CRLF to my real used output.
I just want to know, whether there is a built in method, to remove these blank lines, when including a PHP file. Otherwise I will have to parse this file, remove all CRLF, save it without CRLF and include the modified file afterwards, which is pretty much effort.

use output buffering, if the included script isnt actually generating content (like doesnt have echos etc) use below
ob_start();
include("myscript.php");
ob_end_clean();
if it does generate needed content, the generated content will be in $content.
ob_start();
include("myscript.php");
$content = ob_get_contents();
ob_end_clean();

You could use ob_buffers to fetch the output of the file and send the output to oblivion:
<?php
ob_start();
include('file');
ob_end_clean();
?>

Related

php Format arrays and objects etc output in text logfile

Should be a pretty obvious answer, but I have spent several hours looking at existing similar questions and none are working for me
My code generates logfiles for (manual) debugging etc
If I use print_r($array,TRUE) to capture the output from an array as a string and then echo with <pre> tags to display that on screen, it's really easy to view and understand what's going on.
However, when I write the same info to the logfile, fwrite doesn't preserve the line break and indentation formatting so there is a splurge of info that takes significant amounts of time to make sense of, esp larger arrays and objects.
I have tried using output buffer
$string=print_r($array,TRUE);
ob_start();
echo "<pre>$string</pre>";
$outputBuffer = ob_get_contents();
ob_end_clean();
fwrite($handle,$outputBuffer);
However, all that's now happening is that I see the <pre> tags added into the basic, non-layout output
e.g.
<pre>DOING QUERY: SELECT * FROM event_triggers WHERE DateTime<='2015-09-16 13:04:30'</pre><pre>Completed checking for event triggers</pre>
You can't just add HTML tags to a document, open it in an editor expect HTML tags to be rendered correctly.
You either have to setup your log file as a HTML file (doesn't neccessarily have to be valid, so just add .html to the file name and open it in the browser) or use var_dump to echo out the variables.
Rename file to .html extension and just open with a browser. Browser will detect it with line break html document. <pre></pre> will output like <p></p> in the browser.

PHP Concatenate file output to string

I am rewriting a piece of a program to change it from using "echos" throughout the script to create a large output variable using Heredocs instead, which outputs at the end of the file.
A piece of the script includes another PHP file that directly outputs HTML and has php logic within the HTML it outputs. This file is used by other pieces of the overall program that are not yet being rewritten (due to time constraints).
Is it possible to append the output of another file to an $output variable? I've tried doing this, but it it doesn't work for string appending:
$output .= include 'foo.php';
$output .= file_get_contents('foo.php');
The file_get_contents wrote all the PHP logic directly in HTML, as I suspected it would and the straight 'include' echo'd the HTML as I also expected.
Is there a method to get the output buffer of the file and append to a string?
EDIT: Nevermind the question, I completely forgot about OB_Buffering. Added an answer with my solution, no need to answer this one
I feel stupid. I found the answer 5 minutes after posting, I completely forgot about ob_buffering:
ob_start();
include('./foo.php');
$output .= ob_get_contents();
ob_end_clean();

Why need use ob_start() with include()?

Why is recommended use ob_start() in PHP language when you need include other PHP (or template) file?
This is bad:
require_once( dirname( __DIR__ ) . '/views/file.php' );
This is good:
ob_start();
require_once( dirname( __DIR__ ) . '/views/file.php' );
$output = ob_get_clean();
return $output;
But i don't get why
There is no such recommendation, use it if you think its necessary.
ob_start() tells PHP to start buffering output, any echo or other output by any means will be buffered instead of sent straight to the client.
This can be used for many purposes, one of them is being able to send headers even after producing output, since the output wasn't sent yet thanks to buffering.
In this particular piece of code you have, you are using output buffer to catch the output generated by your included file and storing it to a variable, instead of sending it to the client.
It would be hard to say without understanding a little more about your program, as well as who is telling you to do this. For one thing, the return $output line at the bottom--what are you returning from?
I can think of many reasons you'd want to include scripts this way.
In PHP, the ob_* functions deal with output buffering, that is, capturing anything that gets printed to the page/screen your PHP code is running in as a string.
This may be necessary because a very common pattern in classic PHP is to write straight HTML outside of any <?php tags. When you output text this way, it gets sent directly to the screen, bypassing any intermediate processing that you may want to do with it. It's also possible that a programmer may want to define all of their includes in one place, so that they can be switched out easily, and then reference the text to be output as a variable later in the script.
It may also be a way to prevent includes that don't output any text from accidentally outputting white space, making it impossible to change headers later on in the script. I've had problems before with a large code base in which every include had been religiously closed with a ?> and may or may not have included white space afterward. This would have solved the problem with comparatively little effort.
In programming, there are often many different ways to do things, and there's not always one of them that's "right." The end goal is to create something that does its job, is maintainable, and can be comprehended by other programmers. If you have to write a couple of extra lines of code in pursuit of maintainability, it's worth it down the line.
ob_start() is a function that begins output buffering, ob_get_clean(); flushes the buffer, and it looks like you are returning it from a function.
This allows any print or echo statements to be added to the buffer, and then all stored to a variable and returned and printed elsewhere in your application.

PHP Output buffering contains something before script starts

i have a site, where i buffer some output with
ob_start();
...
and it worked fine until today i updated my debian from an older php5.3 to the latest php5.3.3-7+squeeze8
Now i sometimes have something in the output buffer before i call it the first time
please don't answer things like
"header must be called before any output is sent."
(I know, I work a lot with output buffers)
when i set an extra ob_get_clean(); at the very first line of my script, it works
<?
ob_get_clean();
it seems, like php is creating some output beforehand
if i put the first line
<? print_r(ob_get_clean()); ?>
then i see, that there is an empty string already in the buffer:
""
on all other pages it isn't, there ob_get_clean(); contains
null
is it possible you have some " " in front of your <?php somewhere? or wrong file encoding issue its usually some kind of that nature, check your files and include files.
Now i sometimes have something in the output buffer before i call it
the first time
It'll be much easier if you give us some info about that mysterious data.
perhaps a case of BOM character?
more info here
i found it:
i had no invisible character in front, it was something different: i called ob_end_clean() one time too much:
this was my code, inside a function i call:
function print_something(){
ob_start();
echo some stuff...
echo ob_get_clean();
ob_end_clean(); // this was the bug!
}
it seems, that you can clear your main output buffer ;)

Save an include's output to a string? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Storing echoed strings in a variable in PHP
Suppose I have
<?php include "print-stuff.php"; ?>
print-stuff.php contains PHP/HTML template, which means that when it is included, HTML gets printed. Is there any way to capture that HTML as a string, so that I may save it to use elsewhere?
Moving the include statement elsewhere is not an option, because print-stuff.php also performs logic (creates/modifies variables) that the surrounding code depends on. I simply want to move the file's output, while leaving its logic as is.
You can Output Buffer it to make sure the HTML isn't shown and is instead put into a variable. (PHP will still run, but HTML output will be contained in the variable)
ob_start();
include "print-stuff.php";
$contents = ob_get_contents();
ob_end_clean();
....
Honestly I came on here and went ah-ha, I know the answer to this!!! Then I looked down and saw other people got to it before I did.
But, for the heck of it, I do like this:
ob_start();
include 'something.php';
$output = ob_get_contents();
ob_end_clean();
Have a look at output buffering.
http://us2.php.net/manual/en/function.ob-start.php
You can do that if you print in a buffer instead of stdout.
ob_start();
include 'print-stuff.php';
$printedHTML = ob_get_clean();
$fileStr = file_get_contents('/path/not/url/to/script.php');

Categories