使用 PHP 和 SimpleDOM 或 SimpleXML 修改 html 列表

发布于 2024-10-20 10:56:53 字数 2509 浏览 6 评论 0原文

我有一个具有以下结构的 HTML 片段:

<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 last">
            <a href="alink.html" class="level2 item4 last"><span>Title 4</span></a>
        </li>
    </ul>
</li>

我想将其更改为:

<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>

主要思想是在主 li 元素内的 ul 开头添加一个新项目,克隆 a 标签标题该 li 元素的一部分。我不知道如何使用 SimpleXML 或 SimpleDOM http://code.google.com/p/simpledom / 完成这个任务...有什么想法吗?

问候,

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 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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2024-10-27 10:56:53

新版本:

$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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文