Current Location: Home> Latest Articles> How to Use PHP DOM Extension to Manipulate HTML Elements: A Comprehensive Guide

How to Use PHP DOM Extension to Manipulate HTML Elements: A Comprehensive Guide

M66 2025-06-14

Introduction

DOM (Document Object Model) is a standard object model used for processing XML and HTML documents. For developers, DOM provides a flexible way to extract, modify, or delete elements from HTML pages. This article will introduce how to manipulate HTML page elements using PHP's DOM extension, with practical code examples to help readers better understand these operations.

1. Installing and Enabling the DOM Extension

To use PHP's DOM extension, you need to make sure that the extension is installed and enabled in your PHP environment. Below are the steps to install and enable the DOM extension:

  1. Check the php.ini configuration file to confirm that the following line is present: extension=dom.so (Linux) or extension=dom.dll (Windows). If the line is commented out, uncomment it and restart the web server.
  2. Open a terminal or command prompt, execute the php -m command, and verify that the DOM extension is listed among the loaded PHP extensions.

2. Parsing the HTML Page

The first step in using the DOM extension to manipulate HTML elements is to load the HTML document into a DOM object. This can be achieved using PHP's DOMDocument class. Here's a simple example:

$html = '<html><body><h1>Hello, World!</h1></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);

In this example, we load a simple HTML string into the DOMDocument object for further manipulation.

3. Selecting HTML Elements

Once the HTML is loaded, we can use the DOMXPath class along with XPath expressions to select elements from the page. Below is an example of selecting all

elements:

$xpath = new DOMXPath($dom);
$elements = $xpath->query("//h1");
foreach ($elements as $element) {
    echo $element->nodeValue;
}

In this example, we use the XPath expression "//h1" to select all

elements on the page, then loop through them with a foreach loop and output the text content of each

element.

4. Modifying HTML Elements

After selecting an element, we can modify its content, attributes, or perform other DOM operations. Below is an example of how to modify elements:

$element = $dom->getElementsByTagName("h1")->item(0);
$element->nodeValue = "Hello, PHP!";
$newElement = $dom->createElement("p", "This is a new paragraph.");
$element->parentNode->appendChild($newElement);
$oldElement = $dom->getElementsByTagName("h2")->item(0);
$oldElement->parentNode->removeChild($oldElement);
echo $dom->saveHTML();

In this example, we modify the text of the first

element, add a new

element, and remove a

element from the DOM tree. Finally, we use the saveHTML() method to output the modified HTML.

Conclusion

With PHP's DOM extension, developers can easily manipulate HTML page elements by selecting, modifying, or deleting them. This article introduced the basic usage of the DOM extension through practical code examples, helping you quickly master these operations. Depending on your specific needs, you can further explore and utilize other features provided by the DOM extension in real-world development.