Samul
Samul

Reputation: 2019

Get output of page and do a final "parse" with PHP

I have a really long PHP/HTML page with lots of <?php tags all around the document. It's a really bad PHP code and I need to change some strings like XXX to ZZZ. I could try to change it in the code but it's so bad programmed and has around 7 thousand lines so I think I may try to find another alternative.

I just came out with this idea: is it possible to add a PHP code right at the bottom of the page in such a way that this PHP code "grabs" all the output code above and assign it to a variable so it can be manipulated and at the end it can be echoed?

Example:

<?php

balbalbla

?>

<div>BLABLA XXX</div>
....
blabla
<?php echo "blablXXXabla";>

..

Is it possible to add some PHP code right at the bottom of the code which grabs all of the previos "output" and change it?

Upvotes: 1

Views: 71

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

You need to add code to the top and the bottom:

<?php
ob_start();

//code

$output = ob_get_clean();

$output = str_replace('XXX', 'ZZZ', $output);

echo $output;

This will start a buffer that captures all output with ob_start() and then after all output ob_get_clean() will get the contents and assign to the variable and then clear the buffer. Now just modify the contents of $output and display it.

See Output Buffering Control.

Upvotes: 2

Related Questions