Skip to content Skip to sidebar Skip to footer

Php - How To Programmatically Bake Out Static Html File?

How do you programmatically convert a dynamic PHP file into a static HTML file, that would obviously have all dynamic PHP-related values baked in as static HTML?

Solution 1:

As the beginning of your script place this:

<?php
    ob_start();
?>

At the very end of the script, place this:

<?php$myStaticHtml = ob_get_clean();
    // Now you have your static page in $myStaticHtml?>

Output buffering reference here:

http://php.net/manual/en/book.outcontrol.phphttp://www.php.net/manual/en/function.ob-start.phphttp://www.php.net/manual/en/function.ob-end-clean.php

Solution 2:

Somewhere on the top of your PHP file:

ob_start();

After all the processing:

$output = ob_get_clean();
file_put_contents('filename', $output);

And if you then also want to output it for that process (for instance if you want to write cache on runtime but also show that page to that user:

echo$output;

Solution 3:

<?php

ob_start(); // start output bufferingecho"your html and other PHP"; // write to output buffer

file_put_contents("file.html", ob_get_contents()); // write the contents of the buffer to file

ob_end_clean(); // clear the buffer

Solution 4:

View the HTML source in a browser, and save that.

If you want to do this automatically, then use output buffering.

Solution 5:

You can also do it with wget

For example:

$ wget -rp -nH --cut-dirs=1 -e robots=off http://www.domain.com/

Post a Comment for "Php - How To Programmatically Bake Out Static Html File?"