I'd like it if ob_start() didn't let echo's output to their normal destination and just log their contents instead. But it doesn't seem to be doing that. Any ideas? Here's my code:
<?php
ob_start();
echo 'test';
$out = ob_get_contents();
var_dump($out);
test is still echo'd. It's var_dump'd, as well, but I don't want it to be echo'd.
Any ideas?
Thanks!
The output buffer is automatically flushed at the end of the script, so it's expected behaviour.
You are looking for ob_get_clean(), which returns the current buffer before clearing it:
$out = ob_get_clean();
Related
When I use CocoaRestClient to submit a GET on an HTML website it will return the source for the site. How can I return the same thing as a string (or something to convert into a string or parse) in php? I've tried using
echo $_GET[$url];
but it does not seem to return anything.
Note: the string returned will probably be rather large.
If you are asking about how to make an HTTP request from PHP and get the response as a string, for the simplest cases you can use file_get_contents:
$html = file_get_contents('http://www.google.com');
If you want to do something more configurable then you have to go with curl.
Do you want to prevent sending the source to the visitor's browser and get it as a string instead? You can use output buffer.
Put this line at the beginning of your code:
ob_start();
From now on everything that would normally be sent to the browser will be buffered instead. To end buffering and get contents use:
$out = ob_get_contents(); // this will only get the contents
$out = ob_get_clean(); // this will also empty the buffer
To stop buffering:
ob_end_clean(); // will just stop buffering
ob_end_flush(); // will also echo() current buffer
Output buffer docs here.
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 ;)
Need a bit of PHP help here, the included content is appearing as '1' which means that its true but need it's contents to appear and im unsure why it isn't. Here is a shortened version of a function:
public function content {
$website->content = 'Some content here. ';
ob_start();
include('myfile.php');
$file_content = ob_end_clean();
$website->content .= $file_content;
$website->content .= ' Some more content here.';
echo $website->content;
}
This outputs:
Some content here
1
Some more content here
When I need it to output:
Some content here
'Included file content here'
Some more content here
Any idea's how I can fix this?
Try using ob_get_clean() instead of ob_end_clean().
Both clear the current buffer but ob_end_clean() returns a boolean, whereas ob_get_clean() returns the contents of the current buffer.
Instead of using ob_end_clean modify your existing code into something similar to the below
ob_start();
include('myfile.php');
$file_content = ob_get_contents();
ob_end_clean ();
The reason I'm not voting for ob_get_clean is because I've had times when other developers get confused regarding what is really going on, the function name doesn't really state that the output buffering will end after it's call.
I think that function will be prone to further bugs, therefor using ob_start/ob_end_clean is easier to comprehend and better for future code maintenance.
The trade off between one extra line of code but easier code to understand is well worth it.
You should be using ob_get_clean(), not ob_end_clean(). The former returns a string of the output buffer and empties it, while the latter just does the emptying and returns a bool.
I'm having trouble with setting a variable and then giving it the outcome of a translated string. What am I doing wrong?
# usage: this translates some text into different language.
echo __('some text');
# make a variable and fill it with the outcome of the translated text
$title="echo __('translated content text')";
The first line outputs nicely.
The second line output literaly echo __('translated concent text').
Update
Thanks all. great answers. Gosh how stupid i must ve been, therefore am a bit wiser now:)
$title = __('Colourful train rides'); # works
now experimenting with these endings
ob_end_flush();
ob_end_clean();
Don't include quotes around the function call and don't call echo:
$title = __('translated concent text');
The first line outputs nicely
It sounds like that function echoes (also, its name would be very confusing if it didn't).
You will need to use an output buffer to capture that output.
ob_start();
__echo('translated concent text');
$title = ob_get_contents();
ob_end_clean(); // Thanks BoltClock
You're wrapping the function call in quotes, so it's being outputted literally (instead of invoking the function). Take away the pair of quotes, and you'll have a result closer to what you want:
$title = __echo('translated concent text');
However, for this to work the __echo() function will have to return the string, not just echo it. If you want to catch the output, you'll have to use PHP's Output Buffering.
ob_start();
__echo('translated concent text');
$title = ob_get_contents();
ob_end_clean();
I'm using a PHP library that echoes a result rather than returns it. Is there an easy way to capture the output from echo/print and store it in a variable? (Other text has already been output, and output buffering is not being used.)
You could use output buffering :
ob_start();
function test ($var) {
echo $var;
}
test("hello");
$content = ob_get_clean();
var_dump($content); // string(5) "hello"
But it's not a clean and fun syntax to use. It may be a good idea to find a better library...
The only way I know.
ob_start();
echo "Some String";
$var = ob_get_clean();
You should really rewrite the class if you can. I doubt it would be that hard to find the echo/print statements and replace them with $output .=. Using ob_xxx does take resources.
Its always good practise not to echo data until your application as fully completed, for example
<?php
echo 'Start';
session_start();
?>
now session_start along with another string of functions would not work as there's already been data outputted as the response, but by doing the following:
<?php
$output = 'Start';
session_start();
echo $output;
?>
This would work and its less error prone, but if its a must that you need to capture output then you would do:
ob_start();
//Whatever you want here
$data = ob_get_contents();
//Then we clean out that buffer with:
ob_end_clean();