Current Location: Home> Latest Articles> Complete Guide to Dynamically Creating and Parsing HTML Documents with PHP and XML

Complete Guide to Dynamically Creating and Parsing HTML Documents with PHP and XML

M66 2025-06-17

PHP and XML: How to Create and Parse HTML Documents

In modern web development, HTML is the standard language for building web pages, while PHP is a commonly used server-side scripting language. PHP's powerful features help developers dynamically generate HTML documents and interact with XML-based documents. In this article, we'll demonstrate how to use PHP to create and parse HTML documents, and show how XML can assist in the process.

1. Creating HTML Documents with PHP

In PHP, we can dynamically generate HTML documents through string concatenation. The following example demonstrates how to create a basic HTML document with essential tags:

<?php
// Create HTML document
$html = '<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome to My Page</h1>
<p>This is a paragraph.</p>
<p>Learn Now<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Free PHP Study Notes (Deep Dive)</a>;</p>
</body>
</html>';

// Output HTML document
echo $html;
?>

In the above code, we store the HTML structure as a string in the variable `$html` and output it using the `echo` statement. This successfully generates a dynamic HTML page.

2. Parsing HTML Documents

In addition to creating HTML documents, PHP can also use third-party libraries like simple_html_dom to parse and extract content from HTML documents. Here's an example of how to parse HTML and extract links and images:

<?php
// Include simple_html_dom library
require 'simple_html_dom.php';

// Get HTML document from a URL
$html = file_get_html('https://www.example.com');

// Find all links
$links = $html->find('a');
foreach ($links as $link) {
    echo $link->href . '<br>';
}

// Find all images
$images = $html->find('img');
foreach ($images as $image) {
    echo $image->src . '<br>';
}

// Free resources
$html->clear();
?>

In this code, we use the `require` statement to load the simple_html_dom library and retrieve the HTML content from a specified URL using the `file_get_html` function. Then, we use the `find` method to extract all links and images on the page, and loop through them to display their respective URLs.

3. Using XML to Assist in Parsing HTML Documents

In addition to PHP's native functionality, XML can assist in parsing HTML documents. By converting HTML into XML format, we can take advantage of XML's parsing capabilities for more efficient processing. Here's an example of how to use DOM to parse HTML documents:

<?php
// Get HTML document
$html = file_get_contents('https://www.example.com');

// Create DOM object
$dom = new DomDocument();

// Set DOM parsing options
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;

// Load HTML document
$dom->loadHTML($html);

// Get all links
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
    $href = $link->getAttribute('href');
    echo $href . '<br>';
}

// Get all images
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
    $src = $image->getAttribute('src');
    echo $src . '<br>';
}
?>

In this example, we use the `file_get_contents` function to retrieve the HTML content, then create a DOM object and load the HTML. We then use `getElementsByTagName` to find all the links and images, and retrieve their respective attributes (`href` for links and `src` for images).

Conclusion

By combining PHP and XML, developers can efficiently create, parse, and manipulate HTML documents. Whether dynamically generating HTML pages, extracting data, or using XML to assist in parsing, PHP provides a flexible solution for web development. We hope this article helps you understand and apply the power of PHP and XML in HTML document processing.