title: Get the Results of an Include in a String in PHP?
body: <p>Let's say file test.php looks like this:</p>
  
  <pre><code>&lt;?php
  echo 'Hello world.';
  ?&gt;
  </code></pre>
  
  <p>I want to do something like this:</p>
  
  <pre><code>$test = include('test.php');
  
  echo $test;
  
  // Hello world.
  </code></pre>
  
  <p>Can anyone point me down the right path?</p>
  
  <p>Edit:</p>
  
  <p>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:</p>
  
  <pre><code>// Go through all of the code, execute it, and incorporate the results into the content
  while(preg_match('/&lt;\?php(.*?)\?&gt;/ims', $content-&gt;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-&gt;content = str_replace($phpCodeMatches[0], $output, $content-&gt;content);
  }
  </code></pre>