Current Location: Home> Latest Articles> PHP Class for Generating HTML Files: Dynamically Create and Save HTML Content

PHP Class for Generating HTML Files: Dynamically Create and Save HTML Content

gitbox 2025-06-28

PHP Class for Generating HTML Files: Dynamically Create and Save HTML Content

PHP is a widely used programming language for web development, known for its rich libraries and functions that make dynamic HTML generation straightforward. In this article, we'll introduce how to create an HTMLGenerator class to generate HTML files using PHP's file handling and string manipulation capabilities.

Creating the HTMLGenerator Class

First, we define a class named HTMLGenerator. This class will be responsible for generating HTML file content, and it can be easily managed through simple methods.

class HTMLGenerator {
    private $title;
    private $content;

    public function __construct($title) {
        $this->title = $title;
        $this->content = '';
    }

    public function addSection($sectionTitle, $sectionContent) {
        $this->content .= "<h2>" . $sectionTitle . "</h2>";
        $this->content .= "<p>" . $sectionContent . "</p>";
    }

    public function generateHTML() {
        $html = "<!DOCTYPE html>\n";
        $html .= "<html>\n";
        $html .= "<head>\n";
        $html .= "<title>" . $this->title . "</title>\n";
        $html .= "</head>\n";
        $html .= "<body>\n";
        $html .= $this->content;
        $html .= "</body>\n";
        $html .= "</html>";
        return $html;
    }

    public function saveHTML($filename) {
        $html = $this->generateHTML();
        file_put_contents($filename, $html);
    }
}

Using the HTMLGenerator Class to Generate HTML Files

After creating an instance of the HTMLGenerator class and passing a title to it, we can use the addSection method to add multiple sections of content. Each section will have its own title and body.

$generator = new HTMLGenerator("My Awesome Website");
$generator->addSection("Introduction", "This is an introduction to my website.");
$generator->addSection("Features", "Here are some features of my website.");
$generator->addSection("Contact", "You can contact me through the contact form.");

Saving the Generated HTML File

Once the HTML content is created, we can use the saveHTML method to save the generated HTML to a file:

$generator->saveHTML("index.html");

Generated HTML File Example

Below is the content of the generated HTML file:

<!DOCTYPE html>
<html>
<head>
<title>My Awesome Website</title>
</head>
<body>
<h2>Introduction</h2>
<p>This is an introduction to my website.</p>
<h2>Features</h2>
<p>Here are some features of my website.</p>
<h2>Contact</h2>
<p>You can contact me through the contact form.</p>
</body>
</html>

Conclusion

This article demonstrated how to generate well-structured HTML files using PHP. We created the HTMLGenerator class and showed how to dynamically add section content. Using PHP's file operation functions, we then saved the generated HTML as a local file. This method helps developers quickly create and manage HTML files, while also being flexible enough for easy updates and modifications.