Hello. I have a string with html table tags. The task is to split the line into an array of tags with their contents. Example:
<table> <tr> <td> <table> <tr> <td>Π’Π΅ΠΊΡΡ ΡΡΠ΅ΠΉΠΊΠΈ β1</td> <td>Π’Π΅ΠΊΡΡ ΡΡΠ΅ΠΉΠΊΠΈ β2</td> </tr> </table> </td> <td> <table> <tr> <td></td> </tr> </table> </td> </tr> </table> Found a solution by reference a link :
function walk($output, \DOMNode $node, $depth = 0) { if ($node->hasChildNodes()) { $children = $node->childNodes; foreach ($children as $child) { if ($child->nodeType === XML_TEXT_NODE) { continue; } $output[] = $child->nodeName; $item = walk(array(), $child, $depth + 1); if (!empty($item)) { $output[] = $item; } } } return $output; } $dom = new DOMDocument; $dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); $root = $dom->getElementsByTagName('body')->item(0); $output = walk(array(), $root, 0); Everything works as it should and displays only tags in the following format:
["table",["tr",["td",["table",["tr",["td","td"] ... The question is to display the content (not attributes) of these tags. Type:
["table":"",["tr":"",["td":"",["table":"",["tr":"",["td":"Π’Π΅ΠΊΡΡ ΡΡΠ΅ΠΉΠΊΠΈ β1","td":"Π’Π΅ΠΊΡΡ ΡΡΠ΅ΠΉΠΊΠΈ β2"] ... I tried:
array_push($output, array( $child->nodeName => $child->textContent)); at the exit:
["table":"Π’Π΅ΠΊΡΡ ΡΡΠ΅ΠΉΠΊΠΈ β1Π’Π΅ΠΊΡΡ ΡΡΠ΅ΠΉΠΊΠΈ β2 ...
tdand not all in a row - teran