Parsing HTML in PHP: Overview of Libraries and Tools
Parsing HTML is one of the common tasks for a web developer. Whether it's extracting data from a third-party site (scraping), analyzing your own pages, or automating testing — a reliable tool is essential. PHP, as a server-side language, provides several ways to work with HTML code. In this article, we will cover three of the most popular libraries: PHP Simple HTML DOM Parser, DiDOM, and Symfony DomCrawler. You will learn their capabilities, strengths and weaknesses, and see code examples.
Why is HTML parsing needed?
Built-in PHP functions (file_get_contents, preg_match) are too primitive for complex tasks. Regular expressions quickly become cumbersome and break with the slightest change in HTML structure. Specialized libraries allow you to:
- Access elements via CSS selectors or XPath;
- Traverse the DOM tree like in JavaScript;
- Extract text, attributes, links;
- Modify HTML and save the result.
1. PHP Simple HTML DOM Parser
One of the oldest and most well-known libraries. It works without external dependencies and is suitable for PHP 5.6+. Its main advantage is an intuitive syntax reminiscent of jQuery.
Installation
// Download the simple_html_dom.php file from GitHubrequire_once 'simple_html_dom.php';Usage Example
// Create a DOM object from a URL$html = file_get_html('https://example.com');
// Find all linksforeach($html->find('a') as $link) { echo $link->href . ' - ' . $link->plaintext . "";}
// Extract the h1 title$h1 = $html->find('h1', 0);echo $h1 ? $h1->plaintext : 'Title not found';
// Clear memory$html->clear();Pros and Cons
- + Simplicity and clear API;
- + Does not require Composer;
- - Slow performance with large documents;
- - The project has not been updated for a long time;
- - Does not fully support modern PHP 8.x standards.
2. DiDOM
A modern library actively supported by the community. Fully compatible with PHP 7.4–8.3. It uses CSS selectors and XPath and works many times faster than Simple HTML DOM.
Installation via Composer
composer require imangazaliev/didomUsage Example
use DiDom\\Document;
$document = new Document('https://example.com', true);
// Find all links$links = $document->find('a');foreach ($links as $link) { echo $link->attr('href') . "";}
// Search by class$items = $document->find('.product-title');foreach ($items as $item) { echo $item->text() . "";}
// Extract the first element$firstTitle = $document->first('h2');echo $firstTitle ? $firstTitle->text() : 'No titles';Pros and Cons
- + High performance;
- + Support for CSS selectors of any complexity;
- + Encoding handling;
- - Requires Composer;
- - Documentation is available in Russian but is fragmentary.
3. Symfony DomCrawler
Part of the Symfony ecosystem, but can be used as a standalone library. It provides a powerful API for DOM navigation, data extraction, and form handling.
Installation
composer require symfony/dom-crawlercomposer require symfony/css-selector // for CSS selectorsUsage Example
use Symfony\\Component\\DomCrawler\\Crawler;
$html = file_get_contents('https://example.com');$crawler = new Crawler($