How to Capture PHP Output into a Variable

How do I capture PHP output into a variable?

<?php ob_start(); ?>
<xml/>
<?php $xml = ob_get_clean(); ?>
<input value="<?php echo $xml ?>" />͏͏͏͏͏͏

How to Capture PHP Output into a Variable

And I don't want to change that, because although I'm going to access these PHP scripts' outputs from another PHP script, I'm also planning on accessing them from elsewhere. So, is there a way to fool these PHP scripts into accepting some arguments through Get, then capturing their outputs with the method suggested above?

Your question is a bit unclear as to why you're using cURL in the first place. If your scripts are on the same server, you can simply set the correct $_GET variables and use:

<?php
ob_start( );
// include the file.
$_GET['foo'] = 'bar';
include 'the_file.php';

$output = ob_get_clean( );

If your scripts are located on another server, where include is not viable, you will always have to do a HTTP request to get their contents, regardless of whether your this with cURL or Ajax, or sockets for all I care.

How to capture HTML for a PHP variable

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 I capture the result of an echo() into a variable in PHP?

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...

PHP capture print/require output in variable

You can capture output with the ob_start() and ob_get_clean() functions:

ob_start();
print("abc");
$output = ob_get_clean();
// $output contains everything outputed between ob_start() and ob_get_clean()

Alternatively, note that you can also return values from an included file, like from functions:

a.php:

return "<html>";

b.php:

$html = include "a.php"; // $html will contain "<html>"

Php shell command output on to a variable

Use shell_exec:

$var=shell_exec('ls');

or exec:

exec('ls', $output, $return_var);

# print array
foreach($output as $content){
echo $content . "\n";
}
print "return_var:" . $return_var . "\n";

capturing echo into a variable

Let me preface this by saying:

Be careful with that custom function calling business. I am assuming you know how dangerous this can be which is why you're cleaning it somehow.

Past that, what you want is known as output buffering:

function hello() {
print "Hello World";
}
ob_start();
hello();
$output = ob_get_clean();
print "--" . $output . "--";

(I added the dashes to show it's not being printed at first)

The above will output --Hello World--

Possible to capture PHP echo output?

Yes, using output buffering.

<?php
ob_start(); // Start output buffering

Render::UnorderedList(Class::getItems(), Class::getFields(), true);

$list = ob_get_contents(); // Store buffer in variable

ob_end_clean(); // End buffering and clean up

echo $list; // will contain the contents
?>

In PHP, is there a way to capture the output of a PHP file into a variable without using output buffering?

A little known feature of PHP is being able to treat an included/required file like a function call, with a return value.

For example:

// myinclude.php
$value = 'foo';
$otherValue = 'bar';
return $value . $otherValue;

// index.php
$output = include './myinclude.php';
echo $output;
// Will echo foobar


Related Topics



Leave a reply



Submit