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).
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.
I have dozens of templates elaborated by a web designer. They are primarily HTML, with a few PHP tags and data. The problem is that I need to re-use those templates (tpl.php) to send email, and the email body (through PHPMailer) is a variable. I have succeeded to fill a variable with the HTML output in the code example (adapted). My question is: should I redo all the templates or is this a valid approach?
<?php
print "This will print just the 'hello world' output. I don't need to print the function as it has no return value<br />";
hola_mundo(); // I directly call the function and it outputs HTML to the screen
print "<br />";
// Now the assignment to the variable.
ob_start(); // I silence the output to screen
hola_mundo();
$string = ob_get_contents(); // I capture the buffer
ob_end_clean(); // I restore the output to screen
print "Now I print the string variable to demonstrate it has captured the HTML ";
print $string;
function hola_mundo(){
?><font color="red"<b>HOLA MUNDO CRUEL</b></font><?php
} // function
?>
The more logical approach would be to have this function (which I do not have and should redo for dozens of templates):
<?php
$string = hola_mundo();
print $string;
function hola_mundo(){
$string = '<font color="red"<b>HOLA MUNDO CRUEL</b></font>';
return $string
} // function
?>
Do you mean this?
function getTemplate($file) {
ob_start();
include $file;
return ob_get_clean();
}
// Example usage:
$string = getTemplate('templates/tpl.php');
tpl.php will be executed as a PHP file.
You can even pass variables to the template:
function getTemplate($file, $variables=array()) {
extract($variables);
ob_start();
include $file;
return ob_get_clean();
}
// Example usage:
$string = getTemplate('templates/tpl.php', array('message' => 'Hello world!'));
This will extract 'Hello world' into the function scope, making it available as the $message variable to the template.
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");
I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).
I tried using $html = file_get_content("B590.php");
But then $html will have the content of B90.php as plain text(with php code).
Is there any method where I can get the content of the file after it has been evaluated?
There seems to be many related questions like this one and this one but none seems to have any definite answer.
You can use include() to execute the PHP file and output buffering to capture its output:
ob_start();
include('B590.php');
$content = ob_get_clean();
function get_include_contents($filename){
if(is_file($filename)){
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
$html = get_include_contents("/playbooks/html_pdf/B580.php");
This answer was originally posted on Stackoverflow
If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:
ob_start();
include('B590.php');
$html = ob_get_clean();
Example:
B590.php
<div><?php echo 'Foobar'; ?></div>
current.php
$stuff = 'do stuff here';
echo $stuff;
include('B590.php');
will output:
do stuff here
<div>Foobar</div>
Whereas, if current.php looks like this:
$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;
The output will be:
do stuff here
Some more
<div>Foobar</div>
To store evaluated result into some variable, try this:
ob_start();
include("B590.php");
$html = ob_get_clean();
$filename = 'B590.php';
$content = '';
if (php_check_syntax($filename)) {
ob_start();
include($filename);
$content = ob_get_clean();
ob_end_clean();
}
echo $content;