开发者

modifying html list with PHP and SimpleDOM or SimpleXML

i have a HTML piece with this structure:

<li class="level1 item3 parent">
    <a href="mainlink.html" class="level1 item3 parent">
        <span>
            <span class="title">Main title</span>
            <span class="sub">Subtitle</span>
        </span>
    </a>
    <ul class="level2">
        <li class="level2 item1 first">
            <a href="alink.html" class="level2 item1 first"><span>Title 1</span></a>
        </li>
        <li class="level2 item2">
            <a href="alink.html" class="level2 item2"><span>Title 2</span></a>
        </li>
        <li class="level2 item3">
            <a href="alink.html" class="level2 item3"><span>Title 3</span></a>
        </li>
        <li class="level2 item4开发者_如何学Go last">
            <a href="alink.html" class="level2 item4 last"><span>Title 4</span></a>
        </li>
    </ul>
</li>

I want to change it into this:

<li class="level1 item3 parent">
    <a href="mainlink.html" class="level1 item3 parent">
        <span>
            <span class="title">Main title</span>
            <span class="sub">Subtitle</span>
        </span>
    </a>
    <ul class="level2">
        <li class="level2 item1 first">
            <a href="mainlink.html" class="level2 item1 first"><span>Main title</span></a>
        </li>
        <li class="level2 item1">
            <a href="alink.html" class="level2 item2"><span>Title 1</span></a>
        </li>
        <li class="level2 item2">
            <a href="alink.html" class="level2 item3"><span>Title 2</span></a>
        </li>
        <li class="level2 item3">
            <a href="alink.html" class="level2 item4"><span>Title 3</span></a>
        </li>
        <li class="level2 item4 last">
            <a href="alink.html" class="level2 item5 last"><span>Title 4</span></a>
        </li>
    </ul>
</li>

The main idea is to add a new item at the beginning of the ul inside the main li element, cloning part of the a tag heading that li element. I don't know how to use SimpleXML or SimpleDOM http://code.google.com/p/simpledom/ to acomplish this task... any ideas?

regards,


New version :

$doc = new DomDocument;
$doc->loadHTML($html);

$li = $doc->getElementsByTagName('li')->item(0)->cloneNode(true);

foreach($li->getElementsByTagName('ul') as $ul) {
    $ul->parentNode->removeChild($ul);
}

$span = $li->getElementsByTagName('span')->item(0);
$span->nodeValue = $li->getElementsByTagName('span')->item(1)->nodeValue;
foreach($span->getElementsByTagName('a') as $a) {
    $a->setAttribute('class', 'level2 item1 first');
}

$ul = $doc->getElementsByTagName('ul')->item(0);

$nodes = $ul->getElementsByTagName('a');
$node = $ul->childNodes->item(0);

$i = 2;
foreach($nodes as $n) {
    $n->setAttribute('class', 'level2 item' . $i);
    $i++;
}

$ul->insertBefore($li, $node);

$nodes->item(1)->parentNode->setAttribute('class', 'level2 item1');

echo $doc->saveHTML();

http://codepad.org/pIwwK1Z5

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜