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.
Related
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();
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 ;)
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();
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');