PHP DOM如何从UL获取项目和子项目

问题描述 投票:-1回答:1

我正在尝试从下面的菜单中获取带有锚标签的所有项目和子项目:

<nav class="header-nav" id="headerLara">
	<div class="menu-hauptmenu-container">
		<ul id="head_nav_ul" class="menu">
			<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4">
				<a>First Menu</a>
				<ul class="sub-menu">
					<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-14002">
						<a href="http://example.com/fm1">F menu 1</a>
					</li>
					<li class="menu-item menu-item-type-post_type menu-item-object-post menu-item-12718">
						<a href="http://example.com/fm2">F menu 2</a>
					</li>
				</ul>
			</li>
			<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-6">
				<a>Second Menu</a>
				<ul class="sub-menu">
					<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1257">
						<a href="http://example.com/sm1">S menu 1</a>
					</li>
					<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-5420">
						<a href="http://example.com/sm2">S menu 2</a>
					</li>
				</ul>
			</li>
			<li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-12821">
				<a href="http://example.com/m3">Third Menu</a>
			</li>
		</ul>
	</div>
</nav>

现在,我希望像:

<nav class="header-nav" id="headerLara">
	<div class="menu-hauptmenu-container">
		<ul>
			<li>
				<a class="has-child">First Menu</a>
				<ul>
					<li>
						<a href="http://example.com/fm1">F menu 1</a>
					</li>
					<li>
						<a href="http://example.com/fm2">F menu 2</a>
					</li>
				</ul>
			</li>
			<li>
				<a class="has-child">Second Menu</a>
				<ul>
					<li>
						<a href="http://example.com/sm1">S menu 1</a>
					</li>
					<li>
						<a href="http://example.com/sm2">S menu 2</a>
					</li>
				</ul>
			</li>
			<li>
				<a href="http://example.com/m3">Third Menu</a>
			</li>
		</ul>
	</div>
</nav>

我已经完成了一些研发工作,并尝试了以下PHP代码:

    <?php
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('http://example.com/blabla.php'));
$header = $doc->getElementById('headerLara');

$mainUls = $header->getElementsByTagName('ul');
foreach ($mainUls as $mainUl) {
    echo '<ul>';
    $mainLis = $mainUl->getElementsByTagName('li');
    foreach ($mainLis as $mainLi) {
    echo '<li>';
    $mainAnc = $mainLi->getElementsByTagName('a');
    $href = $mainAnc->item(0)->getAttribute('href');
    echo '<a class="has-child" href="'.$href.'">'.$mainAnc->item(0)->nodeValue.'</a>';   
    $secUls = $mainLi->getElementsByTagName('ul');
    if($secUls->length < 2){
        foreach ($secUls as $secUl) {
            echo '<ul>';
            $secLis = $secUl->getElementsByTagName('li');
            foreach ($secLis as $secLi) {
                echo '<li>';
                $secAnc = $mainLi->getElementsByTagName('a');
                $shref = $secAnc->item(0)->getAttribute('href');
                echo '<a href="'.$shref.'">'.$secAnc->item(0)->nodeValue.'</a>';  
                echo '</li>';
            }
            echo '</ul>';
        }
    }
    echo '</li>';
    }
    echo '</ul>';
}
?> 

但是这对我不起作用,并返回如下输出:

<ul>
	<li>
		<a class="has-child" href="">First Menu</a>
		<ul>
			<li>
				<a href="">First Menu</a>
			</li>
			<li>
				<a href="">First Menu</a>
			</li>
		</ul>
	</li>
	<li>
		<a class="has-child" href="http://example.com/fm1">F menu 1</a>
	</li>
	<li>
		<a class="has-child" href="http://example.com/fm2">F menu 2</a>
	</li>
	<li>
		<a class="has-child" href="">Second Menu</a>
		<ul>
			<li>
				<a href="">Second Menu</a>
			</li>
			<li>
				<a href="">Second Menu</a>
			</li>
		</ul>
	</li>
	<li>
		<a class="has-child" href="http://example.com/sm1">S menu 1</a>
	</li>
	<li>
		<a class="has-child" href="http://example.com/sm2">S menu 2</a>
	</li>
</ul>

我检查了许多链接,这些链接似乎与我的问题相似,但没有任何帮助。

我如何获得适当的输出,在此先感谢。

php list dom domdocument
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.