I have a function ( DoDb::printJsonDG($sql, $db, 1000, 2)
) which echos json. I have to catch it and then use str_replace() before it is send to the user. However I cannot stop it from doing echo. I don't want to change printJsonDG because it is being used in several other locations.
Echo sends output to the output buffer, If it's embedded in HTML (which is also being sent to the output buffer) then it appears that it writes that to the "source".
In PHP, the only way to output text is with echo.
With PHP, there are two basic ways to get output: echo and print .
The echo is used to display the output of parameters that are passed to it. It displays the outputs of one or more strings separated by commas. The print accepts one argument at a time & cannot be used as a variable function in PHP.
You can use the ob_start()
and ob_get_contents()
functions in PHP.
<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>
Will output :
string(6) "Hello "
string(11) "Hello World"
You can do it by using output buffering functions.
ob_start();
/* do your echoing and what not */
$str = ob_get_contents();
/* perform what you need on $str with str_replace */
ob_end_clean();
/* echo it out after doing what you had to */
echo $str;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With