How to capture everything what leaves included file to variable or another file.
Output buffering is the way to go.
<?php
ob_start(); // Start buffering output
include '/path/to/file/';
$myVariable = ob_get_clean(); // Put the buffered output
// into $myVariable and clear
// the output buffer
http://www.php.net/manual/en/function.ob-start.php
http://www.php.net/manual/en/function.ob-get-clean.php
Using output buffers: http://www.php.net/manual/en/function.ob-get-contents.php
Related
I'm trying to send out a voucher, via the PHP's email() function, where I reference an external .php file using include() as the message contents. Heres the code:
$message = '<?php include ("/fullpath/inc/voucher.php"); ?>';
This isn't working because I'm declaring it as a text string, but if I don't it will just print out the contents and not put it as the content of the variable. Any ideas?
You can use the ob_buffer for that, try this:
<?php
ob_start(); // start buffer
include("file.php"); // read in buffer
$var=ob_get_contents(); // get buffer content
ob_end_clean(); // delete buffer content
echo $var; // use $var
?>
Example from http://www.webmaster-eye.de/include-in-Variable-umleiten.210.artikel.html (German)
In this post two solutions depending on the circumstances will be described..
voucher.php is returning a value
If voucher.php has a return statement in it you can safely use include to get a hold of this returned value.
See the below example:
voucher.php
<?php
$contents = "hello world!";
...
return $contents;
?>
...
$message = include ("/fullpath/inc/voucher.php");
voucher.php is printing data that I'd like to store in a variable
If voucher.php is printing data that you'd like to store in a variable you can use php's output buffering functions to store away the printed data and then assign it to $message.
ob_start ();
include ("/fullpath/inc/voucher.php");
$message = ob_get_contents ();
ob_end ();
ob_start();
include ("/fullpath/inc/voucher.php");
$message = ob_get_clean();
However output buffering in most cases is considered as bad practice and not recommended to use
Like in your case you better have a function declared in voucher.php that returns the message as a string. So all the code could be as simple as: $message = get_voucher();
I've encountered the same ploblem, here is the functions that resolves the problem for me. It uses the Tobiask answer
function call_plugin($target_) { // $target_ is the path to the .PHP file
ob_start(); // start buffer
include($target_); // read in buffer
$get_content = ob_get_contents(); // get buffer content
ob_end_clean();
return $get_content;
}
You want to use the readfile() function instead of include.
See: http://en.php.net/manual/en/function.readfile.php
If you want to get the =results= of PHP parsing the voucher.php one way is to make sure that you put voucher.php on a reachable URL, and then call it like:
$message = file_get_contents("http://host.domain.com/blabla/voucher.php");
This would insert the contents/output of the parsed voucher.php into the variable $message.
If you can't reach the voucher.php publically you have to rewrite it to output the string in the end with the return() function, or write a function in voucher.php that outputs the string you want when called after which you include() it and call the function from within this master PHP script.
$message = include '/fullpath/inc/voucher.php';
and withing /fullpath/inc/voucher.php
// create $string;
return $string;
$message=file_get_contents("/fullpath/inc/voucher.php");
How do you programmatically convert a dynamic PHP file into a static HTML file, that would obviously have all dynamic PHP-related values baked in as static HTML?
As the beginning of your script place this:
<?php
ob_start();
?>
At the very end of the script, place this:
<?php
$myStaticHtml = ob_get_clean();
// Now you have your static page in $myStaticHtml
?>
Output buffering reference here:
http://php.net/manual/en/book.outcontrol.php
http://www.php.net/manual/en/function.ob-start.php
http://www.php.net/manual/en/function.ob-end-clean.php
Somewhere on the top of your PHP file:
ob_start();
After all the processing:
$output = ob_get_clean();
file_put_contents('filename', $output);
And if you then also want to output it for that process (for instance if you want to write cache on runtime but also show that page to that user:
echo $output;
<?php
ob_start(); // start output buffering
echo "your html and other PHP"; // write to output buffer
file_put_contents("file.html", ob_get_contents()); // write the contents of the buffer to file
ob_end_clean(); // clear the buffer
View the HTML source in a browser, and save that.
If you want to do this automatically, then use output buffering.
You can also do it with wget
For example:
$ wget -rp -nH --cut-dirs=1 -e robots=off http://www.domain.com/
The easiest way is to open the page and to copy "view source"
You can also use
php function $homepage = file_get_contents('http://www.example.com/');
and save it in a file
From the related posts:
<?php
job_start(); // your PHP / HTML code here
file_put_contents('where/to/save/generated.html', ob_get_clean());
?>
I have some classes I am writing unit tests for which have echoes in them. I want to suppress this output and thought ob_start() and ob_clean() would suffice, but they aren't having an effect.
public function testSomething (){
ob_start();
$class = new MyClass();
$class->method();
ob_clean();
}
I've also tried variations such as ob_start(false, 0, true); and ob_end_clean() to no avail.
What am I missing?
you may want something like this
<?php
public function testSomething (){
ob_start();
ob_implicit_flush(false); // turn off implicit flush
// Make your output below
$class = new MyClass();
$class->method();
// End of output
// store output into variable:
$output = ob_get_contents();
}
?>
Do you have implicit_flush set to true in your PHP ini? This can cause the behaviour you are seeing as it tells PHP to tell the output layer to flush itself automatically after every output block. This is equivalent to calling the PHP function flush() after each and every call to print() or echo() and each and every HTML block.
The following solves this problem for me. Without calling ob_end_clean(), the contents of the buffer remain until the script's end, where it is flushed.
ob_implicit_flush(false);
ob_start();
/*
...
... do something that pushes countent to the output buffer
...
*/
$rendered = ob_get_contents();
ob_end_clean(); // Needed to clear the buffer
Am I allowed to have two or more ob_start(); in my php files if so what is the proper way to end one ob_start(); and start another?
From the manual:
Output buffers are stackable, that is,
you may call ob_start() while another
ob_start() is active. Just make sure
that you call ob_end_flush() the
appropriate number of times. If
multiple output callback functions are
active, output is being filtered
sequentially through each of them in
nesting order.
In addition to stacking (nesting), you can have separate blocks in sequence.
<?
ob_start();
echo "Foo";
ob_end_flush(); // outputs buffer contents and turns off output buffering
ob_start();
echo "Bar";
ob_end_flush();
?>
You are allowed to do more than one ob_start() on a page. You end ob_start() with ob_end_clean().
ob_start();
$postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
ob_end_clean();
echo $postOutput;
I seem to be confused about PHP output buffering. I have code like this:
function return_json($obj) {
ob_get_clean();
ob_start();
header("Content-Type: application/json");
echo json_encode($obj);
exit;
}
But it doesn't seem to like the ob_get_clean(). I do that because some HTML might accidentally get generated before it gets to that point but I thought this was how you were meant to do it.
What am I missing?
To use ob_get_clean (), you have to be sure, that at some point you have ob_start ()'ed earlier. Otherwise, there’s no buffer to clean, everything is already flushed to the user agent.
Use the ob_get_level() function to see if an output buffer is active and quit it:
while (ob_get_level()) {
ob_end_clean();
}
you have to do an ob_start before all your code to catch any output before that function is called
If you just want to clean the buffer after starting output buffering with
ob_start()
use
ob_clean()
Also be aware that nothing is already being flushed with functions like echo, print_r, etc. So the first thing in your script should be ob_start(). Be sure your includes do not already send something to the browser.
ob_start needs to be called before any content is generated. Normal usage would be something like:
ob_start();
# generated content here
$content = ob_get_contents(); # $content now contains anything that has been output already
ob_end_clean();
# generate any headers you need
echo $content;
If the problem you are having is that nothing is going to output, you seem to be missing the flush method? Also, ob_end_clean() can only be called after output buffering has been started, otherwise it returns 'false'. You can't use the ob_ methods to clean up any existing headers that have already been issued, you need to make sure of that yourself.
function return_json($obj) {
ob_start();
header("Content-Type: application/json");
echo json_encode($obj);
ob_end_flush();
exit;
}