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.
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.
<div id="content">
<p>First paragraph</p>
<p>Second paragraph</p>
<span>A span</span>
</div>
You first need to include the library from GitHub or its official site:
include('simple_html_dom.php');
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>');
$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
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
children() only gets child element nodes, excluding text nodes or comments.
If you want to traverse all descendant nodes (not just direct children), you can use find('*').
When using simple_html_dom, ensure to safely handle external HTML input to avoid XSS or other injection risks.
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