strange ob_start() behaviour - double output - php

ob_start() doesn't seem to be stopping any output so when I flush the buffer it's doubling up
<?php
ob_start();
echo "Text..... <br />";
echo ob_get_flush();
?>
Outputs
Text.....
Text.....
But I was expecting
Text.....
Any ideas ?
Thanks

Remove the echo on the last line.
ob_get_flush() implicitly prints the stored output and also returns it so you're printing it out twice.
You may have confused ob_get_flush() with ob_get_clean()

try:
<?php
ob_start();
echo "Text..... <br />";
ob_get_flush();
?>
from http://php.net/manual/en/function.ob-get-flush.php
Flush the output buffer, return it as a string and turn off output buffering
Flush the output means: it sends the output to the browser or the commandline.
return the string means: it returns the string, so you can store the flushed string in a variable. And since you're echoing this string you get the output a second time.

Related

Multiline block showing as one line PHP

I'm just running some example PHP code verbatim, but it's outputting as a single line in my browser. I'm expecting to see new multiple lines.
<?php
$author = "Alfred E Newman";
echo <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_END;
?>
Your browser by default assumes that any output is HTML and when displaying HTML, newline characters are treated like spaces. You'd either need to output HTML with BR or P tags to force newlines or you can send a content-type header to tell the browser that the output you are sending is plain text.
<?php
$author = "Alfred E Newman";
// tell the browser that your output is plain text
header("Content-Type: text/plain");
echo <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_END;
?>
<?php
$author = "Alfred E Newman";
$str = "This is a Headline
This is the first line.
This is the second.
- Written by $author.
";
echo nl2br($str);
?>
will give you what you need;

nl2br() , preg_replace , htmlspecialchars() and mysql_real_escape_string()

I have read all answers related to this but couldn't get the exact order to sanitize data
I have to input
<?php echo 'yes'; ?>
<?php echo 'yes' ?>
into a text area and submit it in database as it is with line breaks and output the code as it is with line breaks just as stackoverflow is doing.
output comes like this
<?php echo \'yes\'; ?>\r\n\r\n<?php echo \'yes\'; ?>
note : htmlspecialchars() is outputting the exact code but without line breaks ...
nl2br() is not taking /r and /n as line breaks
you can also use the below functions when inserting data
$unwanted = array("-", "_", "+"," ", etc .. );
$yourVariable = str_replace($unwanted, "NULL", $yourVariable);
or
trim($yourVariable);
This function returns a string with whitespace stripped from the beginning and end or yourVariable

Output buffering - Echo values dynamically?

I want to echo string dynamically, not all of them at once when script finished running. Tried this one, but it echos all of them when script finished running. How can I echo values dynamically ?
<?php
ob_start();
echo "Line #1...<br>";
ob_flush();
flush();
sleep(2);
echo "Line #2...<br>";
ob_flush();
flush();
sleep(2);
echo "Line #4...<br>";
?>
Try sending a line-ending like \n or append at least 256 spaces to each echo to trigger the browser.
Some browsers will wait for at least 256bytes before rendering, others need a newline char. Try this combination before each flush:
echo str_repeat(" ", 256) . "\n";
Other cause could be the webserver that is caching the response.

PHP exec capture text

I am having a problem with capturing text when I make an exec call to a perl script that just prints a lot of text. What happens when I run the following code is I get a 1 word result: "Array". I need to be able to capture the results so that I can change them around just a little bit. Here is the code:
<?php
$lastline = exec("perl parseOutput.pl",$retVal);
echo $retVal;
?>
How do I work around this?
You have an array of the lines of text that was outputted.
Do something like this:
echo implode( "\n", $retVal);
Or
echo implode( "<br />\n", $retVal);
And you'll see all of the output generated by the perl script.
just use shell_exec()
$fullResult = shell_exec("perl parseOutput.pl");
echo $fullResult;

How to fix PHP preg_replace() faulty result order?

The faulty result I get now is: 17th of July, 2011Today is .
function finclude($file){
include($file);
}
$str = "Today is {include 'date.php'}.";
echo preg_replace("/\{include '(.*)\'}/e", 'finclude("$1")', $str);
date.php :
<?php echo date('jS \of F'); ?>, 2011
Expected result: Today is 17th of July.
function finclude($file){
return include($file);
}
<?php return date('jS \of F'); ?>
Result isn't expected because You print date, then finclude return null, then you print "Today is "+finclude
What you call faulty in your result order is actually caused by the execution order of your statements:
echo preg_replace("/\{include '(.*)\'}/e", 'finclude("$1")', $str);
Will start the output (echo) and then call the preg_replace function. In which you make use of the e - eval modifier to execute code, namely the function finclude.
So finclude get's executed earlier than preg_replace will return it's result.
So if finclude does output on its own, it will be displayed in front of the result of preg_replace.
Knowing this is half the solution to your problem. It's much likely you didn't intend this output order (your expected result differs) and you just wanted to make finclude return a value instead of outputting something. To convert output into a return value you can make use of an output buffer:
function finclude($file){
ob_start();
include($file);
return ob_get_clean();
}
$str = "Today is {include 'date.php'}.";
echo preg_replace("/\{include '(.*)\'}/e", 'finclude("$1")', $str);
This will ensure that every output within finclude will be returned as a return value instead.
That done you can re-use existing code/includes that normally outputs within your search and replace operation. However using the e modifier always is dangerous and it normally should be prevented. So take care.
i think you need to put <?php return date('jS \of F'); ?> in date.php

Categories