Current Location: Home> Latest Articles> How to Use the children Function in PHP to Extract Child Nodes of HTML DOM Elements?

How to Use the children Function in PHP to Extract Child Nodes of HTML DOM Elements?

M66 2025-08-11

When performing web scraping or HTML content parsing, extracting the child nodes of a specific element is a very common task. PHP offers multiple ways to handle HTML, among which using DOMDocument combined with DOMElement is a more standard and powerful method. However, if you use third-party libraries like simple_html_dom, you can more conveniently get child nodes through the children() function.

What is the children() Function?

children() is a method provided by the simple_html_dom library that is used to get all child elements of an HTML element (excluding text nodes). This method returns an array of child nodes or a specific indexed child node.

Sample HTML

<div id="content">
    <p>First paragraph</p>
    <p>Second paragraph</p>
    <span>A span</span>
</div>

How to Use children() to Extract Child Nodes?

Step 1: Include simple_html_dom

You first need to include the library from GitHub or its official site:

include('simple_html_dom.php');

Step 2: Load the HTML

You can load HTML from a string, file, or URL.

$html = str_get_html('<div id="content"><p>First paragraph</p><p>Second paragraph</p><span>A span</span></div>');

Step 3: Select the target element and use children()

$div = $html->find('div#content', 0); // Get the first div with id content
$children = $div->children();         // Get all child nodes
<p>foreach ($children as $child) {<br>
echo $child->tag . ': ' . $child->innertext . "<br>";<br>
}<br>

The output will be:

p: First paragraph  
p: Second paragraph  
span: A span  

Getting a Child Node at a Specific Index

The children() method can also accept a parameter to get the child node at a specific index.

$firstChild = $div->children(0);
echo $firstChild->tag; // Output: p

Notes

  1. children() only gets child element nodes, excluding text nodes or comments.

  2. If you want to traverse all descendant nodes (not just direct children), you can use find('*').

  3. When using simple_html_dom, ensure to safely handle external HTML input to avoid XSS or other injection risks.

Summary

Using the children() function allows you to easily retrieve all child nodes under a specific HTML element, which is very useful when extracting structured data. Combined with simple_html_dom, you can quickly parse and manipulate complex HTML documents, greatly improving development efficiency. For scenarios requiring more powerful DOM manipulation, it is recommended to explore PHP’s built-in DOMDocument class, which offers more control and flexibility.

  • Related Tags:

    HTML