Get the Results of an Include in a String in PHP?
include, php
Solution
Using output buffering is the best bet.
ob_start();
include 'test.php';
$output = ob_get_clean();
PS: Remember that you can nest output buffers to your hearts content as well if needed.
Problem
Let's say file test.php looks like this: ``` <?php echo 'Hello world.'; ?> ``` I want to do something like this: ``` $test = include('test.php'); echo $test; // Hello world. ``` Can anyone point me down the right path? Edit: My original goal was to pull PHP code intermingled with HTML out of a database and process it. Here's what I ended up doing: ``` // Go through all of the code, execute it, and incorporate the results into the content while(preg_match('/<\?php(.*?)\?>/ims', $content->content, $phpCodeMatches) != 0) { // Start an output buffer and capture the results of the PHP code ob_start(); eval($phpCodeMatches[1]); $output = ob_get_clean(); // Incorporate the results into the content $content->content = str_replace($phpCodeMatches[0], $output, $content->content); } ```