Home > Backend Development > PHP Tutorial > How to Extract Text from Elements with a Specific Class in PHP Using DOMDocument?

How to Extract Text from Elements with a Specific Class in PHP Using DOMDocument?

Susan Sarandon
Release: 2024-11-12 10:24:01
Original
345 people have browsed it

How to Extract Text from Elements with a Specific Class in PHP Using DOMDocument?

Flatten Text from Elements with Specified Class

Introduction:

Parsing HTML using PHP DOM can be a challenge, especially when dealing with specific requirements. This article explores a solution to extract text from elements with a designated class and organize it into structured arrays.

Scenario:

Consider the following HTML content:

<p class="Heading1-P">
    <span class="Heading1-H">Chapter 1</span>
</p>
<p class="Normal-P">
    <span class="Normal-H">This is chapter 1</span>
</p>
Copy after login

The goal is to extract the text from elements with the "Heading1-H" class into the $heading array and text from those with the "Normal-H" class into the $content array, resulting in:

$heading = ['Chapter 1', 'Chapter 2', 'Chapter 3'];
$content = ['This is chapter 1', 'This is chapter 2', 'This is chapter 3'];
Copy after login

Solution Using DOMDocument and DOMXPath:

We employ DOMDocument and DOMXPath to tackle this task.

// Load HTML into DOMDocument
$dom = new DOMDocument();
$dom->loadHTML($html);

// Create DOMXPath object
$xpath = new DOMXPath($dom);

// Get elements with desired class using XPath
$xpathQuery = "//*[@class='$class']";
$elements = $xpath->query($xpathQuery);

// Extract text from elements and store in arrays
$headings = [];
$contents = [];
foreach ($elements as $element) {
    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
        $headings[] = $node->nodeValue;
    }
}

var_dump($headings);
Copy after login

This solution effectively parses the HTML and returns the desired arrays.

Note:

Using jQuery for this task is not recommended, as PHP DOM provides a more structured and programmatic approach to HTML manipulation.

The above is the detailed content of How to Extract Text from Elements with a Specific Class in PHP Using DOMDocument?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template