Forum Moderators: coopster

Message Too Old, No Replies

retain PHP code thru buffer?

can you buffer PHP code without processing it?

         

MiddleCity

6:29 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



Hi... I "think" I'm about to ask a PHP buffer question... but let me explain the situation... I currently use a CMS that builds out pages with the following method...

First, the CMS (building page) starts a buffer


ob_start();

Then, it calls a function to pull all the variables ($VARS) I need for this particular page...


getPageVars($id);

Next, it runs through the functions the page needs to build using the $VARS array it just gathered...


HEADER($VARS);
CONTENT($VARS);
FOOTER($VARS);

When that is done, the CMS gathers the contents of the buffer and also stops the buffer...


$bufferContents = ob_get_contents();
ob_end_clean();

With the $bufferContents in hand, the CMS writes the contents to a file at a specified destination...


$filePath=fopen($destination,"w");
fwrite($filePath,$buffer);
fclose($filePath);

And all is well...

However... Due to the fact that all the PHP code gets processed when it goes into the buffer, I am unable to publish any pages that retain dynamic PHP code in them as a final product. In other words, I'd like to be able to use PHP code in the final .php pages written by the CMS... Any clever ideas how to accomplish this without dumping the whole buffer concept?

Thanks for reading my question!

cameraman

7:23 pm on Jan 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would put placeholders in for later extraction:
buffer contents
<!-- my_content -->
other buffer contents

then when it's time to extract:
$output = $buffered_file_retrieval;
$parts = explode('<!-- my_content -->',$output);

$mysubstitution = $whatever_processing_to_do;

$output = $parts[0] . $mysubstitution . $parts[1];
echo $output;

You could also use the str_replace() function:
$output = str_replace('<!-- my_content -->',$mysubstitution,$output);
which method to use would depend on the particulars/scope of the substitution.

Does this make any sense? It's kind of hard to detail in a generic way.

MiddleCity

8:45 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



Thanks! That does makes sense... If I understand correctly, both methods modify the content of the final page AFTER the buffering process, but BEFORE the content was laid into to a .php page at its final destination.

I imagine it's probably easier to manage if I make the substitution code an include() function. The include() function should work as normal once the modified buffer output is writen to the .php file. (right?)

With that solution in hand, know of any other methods to send php thru a buffer without processing?

Thanks again, appreciate your help!