在 DOMXPath 的开头和结尾处将 HTML 字符串附加到 DOMDocument

Bri*_*man 3 html php xpath dom

这是我的 html 块

$html = '<div class="importantnoticediv">
            <p class="important-notice-title important-notice-title-1">
                NOTICE:
            </p>
            <p class="important-notice-title important-notice-title-2">
                PROFESSIONAL INSTALLATION IS RECOMMENDED!
            </p>
        </div>';
Run Code Online (Sandbox Code Playgroud)

我可以DOMDocument使用以下命令启动一个新的目标并针对此特定 DIV 类DOMXpath

$dom = new DOMDocument;
$dom->loadHTML($post_content);
$xpath = new DOMXpath($dom);
$importantnotice = $xpath->query('//div[contains(@class, "importantnoticediv")]');
$indiv = $importantnotice->item(0);
Run Code Online (Sandbox Code Playgroud)

不过,我很难弄清楚如何将我想要的 HTML 附加到此类中。

这是我试图将我的 html 块转换成的内容:

<div class="importantnoticediv">
    <div class="note-icon note-icon-important">
        <span><i class="fa fa-exclamation" aria-hidden="true"></i>
        </span>
    </div>
    <div class="note-text note-text-important">
        <p class="important-notice-title important-notice-title-1">
                NOTICE:
        </p>
        <p class="important-notice-title important-notice-title-2">
                PROFESSIONAL INSTALLATION IS RECOMMENDED!
        </p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

所以,换句话说,

<div class="note-icon note-icon-important">
    <span><i class="fa fa-exclamation" aria-hidden="true"></i>
    </span>
</div>
<div class="note-text note-text-important">
Run Code Online (Sandbox Code Playgroud)

紧接着我的<div class="importantnoticediv">以及</div>在这个 div 类的最后添加一个结束语。

这对于DOMDocumentand来说是可能的吗DOMXpath?鉴于我尝试添加的内容不是“有效”html(即使是,一旦我执行了这两个步骤..)我知道我可以创建新元素,但如何在之后立即插入自定义 html 字符串我选择的开始和结束DOMDocument/ DOMXpath

Dec*_*ler 5

这是一种方法。它利用DOMDocumentFragment. 不可否认,这一切都有点复杂。

此外,它的一个问题DOMDocumentFragment是,appendXML()它没有对应的方法appendHTML(),这意味着只有当它验证为 XML 时,您才能将 HTML 附加到它。

$html = '<div class="importantnoticediv">
            <p class="important-notice-title important-notice-title-1">
                NOTICE:
            </p>
            <p class="important-notice-title important-notice-title-2">
                PROFESSIONAL INSTALLATION IS RECOMMENDED!
            </p>
        </div>';

$dom = new DOMDocument;
// format our output to make it a tad bit easier on the eyes (doesn't help all that much, though)
$dom->formatOutput = true;
// this implicitly adds <html> and <body> tags
$dom->loadHTML( $html );

$xpath = new DOMXpath( $dom );

// notice I appended /p to your original query, so that we get the list of <p> elements inside the "importantnoticediv"
$nodes = $xpath->query('//div[contains(@class, "importantnoticediv")]/p');
// the first <p>
$p1 = $nodes->item( 0 );
// the second <p>
$p2 = $nodes->item( 1 );

// we create a DOMDocumentFragment that is associated with our DOMDocument
$frag = $dom->createDocumentFragment();

// I append your first HTML fragment (must be valid XML)
$frag->appendXML( '<div class="note-icon note-icon-important">
    <span><i class="fa fa-exclamation" aria-hidden="true"></i>
    </span>
</div>' );
// ... and insert it before our first <p> (parentNode is the "importantnoticediv")
$p1->parentNode->insertBefore( $frag, $p1 );

// frag is now empty again, because we just inserted it(s contents) into the DOMDocument

// we create our second fragment
$frag->appendXML( '<div class="note-text note-text-important"></div>' );

// this time we append it to "importantnoticediv" (i.e. as last child)
// and get the reference to this newly inserted <div>
$insertedNode = $p1->parentNode->appendChild( $frag );

// now we can append (i.e. move) the <p> elements to the newly inserted <div>
$insertedNode->appendChild( $p1 );
$insertedNode->appendChild( $p2 );

// the HTML should now be as desired
echo $dom->saveHTML();
Run Code Online (Sandbox Code Playgroud)