Dom_Document_Helper
in package
This class should guarantee that our work with the DOMDocument is unified and safe.
Table of Contents
- $dom : DOMDocument
- Instance of the DOMDocument.
- __construct() : mixed
- Constructor.
- find_element() : DOMElement|null
- Searches for the first appearance of the given tag name.
- find_elements() : array<int, DOMElement>
- Returns every element matching the given tag name, in document order.
- get_attribute_value() : string
- Returns the value of the given attribute from the given element.
- get_attribute_value_by_tag_name() : string|null
- Searches for the first appearance of the given tag name and returns the value of specified attribute.
- get_element_inner_html() : string
- Returns the inner HTML of the given element.
- get_outer_html() : string
- Returns the outer HTML of the given element.
- get_root_html() : string
- Serializes every top-level node of the loaded fragment back to HTML.
- remove_element() : void
- Removes the given element from the document.
- load_html() : void
- Loads the given HTML content into the DOMDocument.
Properties
$dom
Instance of the DOMDocument.
private
DOMDocument
$dom
Methods
__construct()
Constructor.
public
__construct(string $html_content) : mixed
Parameters
- $html_content : string
-
The HTML content to load.
Return values
mixed —find_element()
Searches for the first appearance of the given tag name.
public
find_element(string $tag_name) : DOMElement|null
Parameters
- $tag_name : string
-
The tag name to search for.
Return values
DOMElement|null —find_elements()
Returns every element matching the given tag name, in document order.
public
find_elements(string $tag_name) : array<int, DOMElement>
Parameters
- $tag_name : string
-
The tag name to search for.
Return values
array<int, DOMElement> —get_attribute_value()
Returns the value of the given attribute from the given element.
public
get_attribute_value(DOMElement $element, string $attribute) : string
Parameters
- $element : DOMElement
-
The element to get the attribute value from.
- $attribute : string
-
The attribute to get the value from.
Return values
string —get_attribute_value_by_tag_name()
Searches for the first appearance of the given tag name and returns the value of specified attribute.
public
get_attribute_value_by_tag_name(string $tag_name, string $attribute) : string|null
Parameters
- $tag_name : string
-
The tag name to search for.
- $attribute : string
-
The attribute to get the value from.
Return values
string|null —get_element_inner_html()
Returns the inner HTML of the given element.
public
get_element_inner_html(DOMElement $element) : string
Parameters
- $element : DOMElement
-
The element to get the inner HTML from.
Return values
string —get_outer_html()
Returns the outer HTML of the given element.
public
get_outer_html(DOMElement $element) : string
Parameters
- $element : DOMElement
-
The element to get the outer HTML from.
Return values
string —get_root_html()
Serializes every top-level node of the loaded fragment back to HTML.
public
get_root_html() : string
The document is loaded with LIBXML_HTML_NOIMPLIED (no implicit html/body wrapper), so the top-level nodes are the fragment's own roots. Useful for reading back what remains after elements have been removed.
Return values
string —remove_element()
Removes the given element from the document.
public
remove_element(DOMElement $element) : void
A no-op when the element has already been detached (its parent is null), so removing the same node twice — e.g. two images sharing one wrapper — is safe.
Parameters
- $element : DOMElement
-
The element to remove.
Return values
void —load_html()
Loads the given HTML content into the DOMDocument.
private
load_html(string $html_content) : void
Parameters
- $html_content : string
-
The HTML content to load.
