I want to convert html syntax into a node tree ( <ul>
structure). How do I do this using the DOMDocument class?
$html = '
<div>
<p>
<a>
</p>
</div>
';
result:
<ul>
<li>
div
<ul>
<li>
p
<ul>
<li>a</li>
</ul>
</li>
</ul>
</li>
</ul>
<?php
$xml = '<div>
<p>
<a>#</a>
</p>
</div>
';
function xml2array($xml,&$result = '') {
foreach($xml->children() as $name => $xmlchild) {
xml2array($xmlchild,$result);
}
$result = "<ul><li>".$xml->getName().$result."</li></ul>";
}
$result='';
$dd = xml2array(simplexml_load_string($xml),$result);
echo "<pre>";
print_r($result);
output
<ul>
<li>
div
<ul>
<li>
p
<ul>
<li>
a
</li>
</ul>
</li>
</ul>
</li>
</ul>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.