- Forums
- HTML
- How To Save The Entire HTML Output Code With A PHP Script
i wrote this page because i didnt know that you could capture the entire HTML output of a dynamic page and save it into a file using PHP [747], Last Updated: Sat May 18, 2024
wallpaperama
Sun Dec 21, 2008
15 Comments
9878 Visits
ok, lets say you have a dynamic website and since php is a server side programming language, when you visit a php website all you get is the html output. so today i wanted to know how i can save the html code into a string or a file.
well, you are in luck Mi Amigo!, because i am going to show you how you can do this.
lets say i have a script and i want to capture, get and save the html only from the script. so the best way to learn is to actually do it.
the first step is to create a php script. im going to keep it simple, if you have a php website, im sure its going to be more complicate, but just remember that whatever you echo or print out, you are going to be able to save the html code only. so what im going to do is capture that html code and declare it into a string. remember, im keeping it simple here. so lets begin,
lets say i have a script called date.php and this file shows me the current date. and this is how my php script looks like:
date.php
<?php
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Wallpaeprama Date</title>
</head>
<body>
<!-- THIS SCRIPT TUTORIAL GUIDE CREATED BY WALLPAPERAMA.COM -->
<h2>Today's Date is: '.date("D M d, Y G:i a").'</h2>
</body>
</html>';
?>
1. step one is to copy and paste the code above into your favorite text editor. im using windows xp, so ill be using notepad.
2. once you have copied and paste the code above, save the file as date.php
3. now upload to your website and open it with your browser, if you have php on your website, you should see a simple message that gives you the current time.. but if you look at the html source code, you will see all the html tags. and this is what we are going to capture and save.
4. ok, now comes the good stuff. if you still have date.php file open, we are going to edit. but putting the following php code at the top:
ob_start();
5. now we put this at the end of the script:
$HtmlCode= ob_get_contents();
ob_end_flush();
6. now save it and upload date.php to your website and you will see that the value of the $HtmlCode string, is the complete html source code.
7. this is how the new edited date.php should look like:
<?php
ob_start();
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Wallpaeprama Date</title>
</head>
<body>
<!-- THIS SCRIPT TUTORIAL GUIDE CREATED BY WALLPAPERAMA.COM -->
<h2>Today's Date is: '.date("D M d, Y G:i a").'</h2>
<p><a href="http://www.wallpaperama.com">Script created by wallpaperama.com</a><p>
</body>
</html>';
$HtmlCode= ob_get_contents();
ob_end_flush();
echo '<hr>This is the value of $HtmlCode: <br><pre style="color:blue;">'.htmlentities($HtmlCode).'</pre>';
?>
hope this helps