How do you append something to the beginning of the output buffer?
For example, say you have the following code:
ob_start();
echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
Before flushing the contents to the browser, how can I append something so that it appears before <p>Start of page.</p> when the page loads?
It sounds simple enough, like moving the pointer to the beginning of an array, but I couldn't find how to do it with the output buffer.
** PHP 5.3 **
ob_start(function($output) {
$output = '<p>Prepended</p>'.$output;
return $output;
});
echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
** PHP < 5.3 **
function prependOutput($output) {
$output = '<p>Appended</p>'.$output;
return $output;
}
ob_start('prependOutput');
echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
Use 2 ob_start commands and ob_end_flush() after what you want to first display then end the buffer with ob_end_flush again when you want to output the rest of the page.
eg:
ob_start();
ob_start();
echo '<p>Start of page.</p>';
ob_end_flush();
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
ob_end_flush();
You can get content of buffer using ob_get_contents() function
ob_start();
echo "World! ";
$out1 = ob_get_contents();
echo "Hello, ".$out1;
Are you wanting it before any output at all? If so, then you're looking for the auto_prepend_file directive. http://php.net/manual/en/ini.core.php
See the first parameter of ob_start (documentation here), it allows you to provide a callback to be called when the buffer is flushed or cleaned. It receives a string as a parameter and outputs a string, therefore it should be easy to
function writeCallback($buffer)
{
return "Added before " . $buffer;
}
ob_start("writeCallback");
Related
my function where I am returning the value but when I am calling this function it also echo the other function.
function myFunction(){
$output1 = anotherFunc();
$output2 = " Hello World!";
return $output2;
}
function anotherFunc(){
echo "Hey this is paragraph";
}
$data = myFunction();
print_r($data);
<?php
function myFunction(){
ob_start();
$output1 = anotherFunc();
ob_end_clean();
$output2 = " Hello World! $output1";
return $output2;
}
function anotherFunc(){
echo "Hey this is paragraph";
return 5;
}
echo myFunction();
ob_start - This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
ob_end_clean - This function discards the contents of the topmost output buffer and turns off this output buffering. If you want to further process the buffer's contents you have to call ob_get_contents() before ob_end_clean() as the buffer contents are discarded when ob_end_clean() is called.
Does PHP give access to all the strings that have been outputted to a page?
<html>
<body>
Link
<?php
echo 'hello world';
echo 'something else';
$s = get_all_output(); // does this exist ?
Of course I could replace every instance of echo 'some text'; by an initial $s = ''; and then $s .= 'some text';.
But without this trick, how to get the current page as a string?
You could make use of the output buffering of PHP (cf. https://www.php.net/manual/en/function.ob-get-contents.php).
<?php
ob_start(); // turn on buffering
echo "Hello ";
$out = ob_get_contents();
ob_end_clean(); // end buffering and clear buffer without "displaying it".
// process $out which contains "Hello "
echo "$out";
You must insert:
ob_start();
at the beginig of your php code and then
$s = ob_get_contents();
at the point where you want to get the content of the page.
Suppose if I have a PHP script test.php which have a method
<?php
function execute($filename){
//do something
return $output;
}
?>
and I also have another PHP script executable.php
<?php
echo "I am executed";
?>
then can i run any code to excute the second file and return the output from the first method execute when i call echo execute('executable.php'); ?
I guess you guys can understand what I meant.
You can use output buffering, as long as the file being included doesn't already do that:
ob_start();
require $filename;
$content = ob_get_contents();
ob_end_clean();
return $content;
Use ob_Start and ob_get_contents to capture the output of the script. Something like this should work:
<?php
function execute($filename){
ob_start();
include $filename;
$output = ob_get_contents();
ob_end_clean();
return $output;
}
<?php#test.php
include 'executable.php';
echo $test;
?>
<?php#executable.php
$test = "I am executed";
?>
function execute($filename){
include_once($filename);
}
$filename is the name of your file to be included.. I think this will help you...
This is the function call..
execute('abc.php');
This question already has answers here:
HTML into PHP Variable (HTML outside PHP code)
(7 answers)
Closed 4 years ago.
Hi i'd like to store a dinamically generated(with php) html code into a variable and be able to send it as a reply to an ajax request.
Let's say i randomly generate a table like:
<?php
$c=count($services);
?>
<table>
<?php
for($i=0; $i<$c; $i++){
echo "<tr>";
echo "<td>".$services_global[$i][service] ."</td>";
echo "<td>".$services_global[$i][amount]."</td>";
echo "<td>€ ".$services_global[$i][unit_price].",00</td>";
echo "<td>€ ".$services_global[$i][service_price].",00</td>";
echo "<td>".$services_global[$i][service_vat].",00%</td>";
echo "</tr>";
}
?>
</table>
I need to store all the generated html code(and the rest) and echo it as a json encoded variable like:
$error='none';
$result = array('teh_html' => $html, 'error' => $error);
$result_json = json_encode($result);
echo $result_json;
I could maybe generate an html file and then read it with:
ob_start();
//all my php generation code and stuff
file_put_contents('./tmp/invoice.html', ob_get_contents());
$html = file_get_contents('./tmp/invoice.html');
But it sounds just wrong and since i don't really need to generate the code but only send it to my main page as a reply to an ajax request it would be a waste of resources.
Any suggestions?
You don't have to store it in a file, you can just use the proper output buffering function
// turn output buffering on
ob_start();
// normal output
echo "<h1>hello world!</h1>";
// store buffer to variable and turn output buffering offer
$html = ob_get_clean();
// recall the buffered content
echo $html; //=> <h1>hello world!</h1>
More about ob_get_clean()
if the data is so much expensive to regenerate then I would suggest you to use memcached.
Otherwise I would go regenerate it every-time or cache it on the frontend.
for($i=0;$i<=5;$i++)
{
ob_start();
$store_var = $store_var.getdata($i); // put here your recursive function name
ob_get_clean();
}
function getdata($i)
{
?>
<h1>
<?php
echo $i;
?>
</h1>
<?php
ob_get_contents();
}
I want to be able to delete some output buffer parts. So that considering this code:
function over_all($string) {
if (OVER_ALL_ENABLED) {
# Stop output buffer
# Clean output buffer
echo $string;
# Restart output buffer
}
}
ob_start();
echo 'This is phrase 1<br>';
over_all('I don\'t want to see Phrase 1<br>');
echo 'This is phrase 2';
over_all('I don\'t want to see Phrase 2');
If I set OVER_ALL_ENABLED to true it will print:
I don't want to see Phrase 1
I don't want to see Phrase 2
else:
This is phrase 1
This is phrase 2
Could you replace comments with correct PHP functions?
Jeff, I think that this code must work
function over_all($string) {
if (OVER_ALL_ENABLED) {
ob_end_clean();
echo $string;
ob_start();
}
}
But if you run it under web server, and gz enable in it, that it will be buffering there
Use ob_end_clean()
Use Ob_Clean() to erase anything that is in the output buffer.
Your function should look like this then:
function over_all($string) {
if (OVER_ALL_ENABLED) {
Ob_Clean ();
echo $string;
}
}
I imagine, it should be something like:
$new_output = "";
function over_all($string) {
if (OVER_ALL_ENABLED) {
$new_output .= $string;
Ob_Clean ();
}
};
ob_start();
echo 'This is phrase 1<br>';
over_all('I don\'t want to see Phrase 1<br>');
echo 'This is phrase 2';
over_all('I don\'t want to see Phrase 2');
echo $new_output;
ob_end_flush();
the $new_output variable will be like you would have a second buffer (one that is not cleaned by ob_clean).