Easiest way to convert a PHP page to static HTML page

12,743

Solution 1

You can use output buffers. If you have an html page, such as:

   <html>
      <head>
         <title>Blah</title>
      </head>
      <body>
        Some text here
      </body>
   </html>

Then if you put, at the top of the html file:

<?php ob_start(); ?>

And right at the bottom, after the last tag, put:

<?php 
   $string = ob_get_contents(); 

   //do whatever you need to do to the html, save it to a seperate file, email it, etc

   ob_flush();
?>

What this basically means is that the $string variable will end up with the entire static html of the page, after it has been dynamically generated. You can then use that string in an email. Although really, html pages don't work exactly the same in emails, so you may want to rethink the approach.

Solution 2

This is a hard thing to do automatically for several reasons:

  1. If your thought HTML support in browsers was bad, email programs are an order of magnitude worse. Basically you should write HTML like it was 1999, so HTML 3.2/4.0, no CSS;
  2. You have a choice of including images as external links or embed them directly in the email. External links take up less space but many mailers block them as they're used by spammers to tag live addresses (by making each image URL they send unique and thus they can figure out which email was opened). Embedded images use a slightly different reference format;
  3. CSS support should basically be treated as nonexistant. All CSS must be internal;
  4. It is best practice, when sending HTML email, to also send a plain-text version for clients that either don't have HTML support or the user has disabled it (yes this does happen). And there is no good way of turning a complex HTML page into a plain text equivalent. It basically has to be done by hand; and
  5. Email content is different to Webpage content. Webpages typically have search boxes, menus, sidebars, headers, footers and so on. These are all things you're not interested in emailing. You're only interested in the content of the page. So blogs work quite well for this because the content of a blog post can easily be extracted. It's harder with arbitrary pages.

So there are various ways you could do this like using cURL to get the Webpage, using output buffering to capture the page by using require/include, etc but all these methods suffer from one or more of the above problems. I've generally found the only way to do HTML email is to hand-roll it.

Solution 3

just do this in php page

Put this on top for where you want to start capturing output:

ob_start();

put this on the bottom of the php

$HtmlCode= ob_get_contents(); ob_end_flush();

$fh=fopen('index.html','w'); fwrite($fh,$HtmlCode);

fclose($fh);

then redirect to html page

Share:
12,743

Related videos on Youtube

Yogi Yang 007
Author by

Yogi Yang 007

I am a software developer and I learn a lot from other peoples work and by observing. I work on part-time basis as a consultant to Software developing companies. I have mastery in UI designing, I have got good hold on VB6, Delphi, VB.NET, PHP, RealBASIC, PureBasic, C#, KBasic &amp; Delphi. I also know Python and PowerBasic. Enough to get work done. ;)

Updated on June 04, 2022

Comments

  • Yogi Yang 007
    Yogi Yang 007 almost 2 years

    I want to convert a web pages which is heavily CSS styled is written in PHP to static html so that I can embed it in an email.

    I have managed to do this but for achieving this I had to convert the whole page into a string and then assign that string as email body. The layout of the page does not look as good as original as I have not been able embed CSS.

    This approach has a few problems like if any changes have to be made to the layout of the page I have to redo the whole process for generating an embeddable page.

    I want an easier way which will allow me to keep the original page editable in Dreamweaver visually.

    • Alistair Evans
      Alistair Evans almost 15 years
      Is this question asking - "How do i get the static output of a php page", or "How to do i format html to work in emails?" The two answers combat different questions.
  • Yogi Yang 007
    Yogi Yang 007 almost 15 years
    That is bad news for me. I have already done the hard work by converting everything by hand but our client keeps on asking for a few layout changes every few days.
  • Yogi Yang 007
    Yogi Yang 007 almost 15 years
    This did the trick. Thanks for this tip. You have saved me hours of untold brain dead work.