Home > Backend Development > PHP Tutorial > How Can I Retrieve Inner and Outer HTML from a DOM Node in PHP?

How Can I Retrieve Inner and Outer HTML from a DOM Node in PHP?

Patricia Arquette
Release: 2024-12-26 12:56:14
Original
628 people have browsed it

How Can I Retrieve Inner and Outer HTML from a DOM Node in PHP?

DOM Manipulation in PHP: Retrieving Inner and Outer HTML

In PHP's DOM implementation, retrieving the inner or outer HTML of a DOM node is not straightforward. Unlike other development environments, PHP lacks native functions specifically designed for this purpose.

Solution

To overcome this, developers have devised reliable methods using DOM traversal and string manipulation. Here's a solution that accurately extracts both inner and outer HTML:

function DOMinnerHTML(DOMNode $element) 
{ 
    $innerHTML = ""; 
    $children  = $element->childNodes;

    foreach ($children as $child) 
    { 
        $innerHTML .= $element->ownerDocument->saveHTML($child);
    }

    return $innerHTML; 
}

// Usage for outerHTML:
// Recursively call DOMinnerHTML with parent as a parameter
Copy after login

Example:

$dom= new DOMDocument(); 
$dom->preserveWhiteSpace = false;
$dom->formatOutput       = true;
$dom->load($html_string);

$domTables = $dom->getElementsByTagName("table"); 

// Iterate over DOMNodeList (Implements Traversable)
foreach ($domTables as $table) 
{ 
    echo DOMinnerHTML($table); 
}
Copy after login

Note:

This solution leverages the saveHTML() method for node traversal and HTML extraction. It iterates through the child nodes of the specified element, recursively processing them to build the resulting inner or outer HTML.

The above is the detailed content of How Can I Retrieve Inner and Outer HTML from a DOM Node in PHP?. 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