<?php
$codes = <<<'CODES'
[column]
[row]
[column][/column]
[column][/column]
[/row]
[/column]
CODES;
$html = str_replace(['[', ']'], ['<', '>'], $codes);
libxml_use_internal_errors(true);
$dom = new \DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
foreach($dom->getElementsByTagName('row') as $row) {
foreach($row->getElementsByTagName('column') as $column) {
$column->appendChild($dom->createTextNode('test'));
$column->setAttribute('class', 'test');
}
}
$tags = getTags($dom->documentElement);
echo var_export($tags, true);
function getTags($element, $tags = [])
{
$tag = ['tagName' => $element->tagName];
if ($element->hasAttributes()) {
foreach ($element->attributes as $attribute) {
$tag['attributes'][$attribute->name] = $attribute->value;
}
}
if ('' !== ($nodeValue = trim($element->textContent)) && false === $element->hasChildNodes()) {
$tag['nodeValue'] = $nodeValue;
}
if ($element->hasChildNodes()) {
foreach ($element->childNodes as $childElement) {
if ($childElement->nodeType !== XML_ELEMENT_NODE) {
continue;
}
$tag[] = getTags($childElement, $tags);
}
}
$tags[] = $tag;
return $tags;
}
- Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array (
0 =>
array (
'tagName' => 'column',
0 =>
array (
0 =>
array (
'tagName' => 'row',
0 =>
array (
0 =>
array (
'tagName' => 'column',
'attributes' =>
array (
'class' => 'test',
),
),
),
1 =>
array (
0 =>
array (
'tagName' => 'column',
'attributes' =>
array (
'class' => 'test',
),
),
),
),
),
),
)
preferences:
60.54 ms | 408 KiB | 5 Q