title: php buffering with ob_start: synchronous alternative
body: <p>Hy community</p>
  
  <p>I have some troubles with php and could not find a solution. I am currently developing a wordpress plugin, and what I would like to do is to manipulate some content. Using php buffering (<code>ob_start</code>) which works fine, but gives me some new troubles. What I am doing is the following (minimalized).</p>
  
  <p>Let's assume that my webpage contains the following text:</p>
  
  <pre><code>Hy there, my name is A B, I am living in C with my dog D
  </code></pre>
  
  <p>What the php code is doing: replace a set of strings with the output of a subfunction. Of course, this is just a minimal example.</p>
  
  <pre><code>&lt;?php
  // -----------------------------------------
  // The Function loading some content from a php file
  function my_function() {
      ob_start();
      require_once("some_file.php");
      $content = ob_get_contents();
      ob_end_clean();
      return($content); // Returns new content
  }
  // -----------------------------------------
  
  // Content of the web page
  $content = "Hy there, my name is A B, I am living in C with my dog D";
  
  // Strings to replace (by the content returned by "my_function")
  $matches = array("A","B","C","D");
  
  // Looping over the different matches
  foreach ( $matches as $match ) {
  
      // Calling my_function in buffer mode
      $content = str_replace($match,call_user_func("my_function"),$content);
  
  
  } ?&gt;
  </code></pre>
  
  <p>Well, what happens now is that the buffering is per definition async. As soon as the first <code>my_function</code> call is finished, the whole buffer will be cleaned and "<code>A</code>" is not replaced by what "<code>A</code>" should be replaced, but also contains parts of what "<code>B</code>" should be replaced with :). If there is only one thing to replace, that works excellent (only having one <code>ob_start</code> process).</p>
  
  <p>Is there any other way to catch output of a include or require call, or to run <code>ob_*</code> in a synchronous way? Maybe there would be a way nicer way I have not found. Would be great to get a hint :).</p>
  
  <p>Thanks in advance! Maybe I am completely on the wrong track, but that's how things are getting learned :).</p>
  
  <p>Happy easter,
  Reto</p>