【问题标题】:Append HTML Strings to DOMDocument at start and end of DOMXPath在 DOMXPath 的开头和结尾处将 HTML 字符串附加到 DOMDocument
【发布时间】:2017-11-09 07:39:20
【问题描述】:

这是我的 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>';

我能够启动一个新的DOMDocument 并使用DOMXpath 定位这个特定的 DIV 类

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

不过,我很难弄清楚如何将我想要的 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>

也就是说,

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

紧跟在我的&lt;div class="importantnoticediv"&gt; 之后,并在这个 div 类的最后添加一个结束 &lt;/div&gt;

DOMDocumentDOMXpath 是否也能做到这一点?鉴于我要添加的不是“有效” html(即使是,一旦我做了这两个步骤..) 我知道我可以创建新元素,但是如何在我选择的DOMDocument/DOMXpath 的开头和结尾之后立即插入一个自定义 html 字符串?

【问题讨论】:

    标签: php html dom xpath


    【解决方案1】:

    这是一种方法。它使用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();
    

    【讨论】:

    • 喜欢它,按描述工作。欣赏cmets!便于跟随和学习。
    猜你喜欢
    • 2019-08-25
    • 2017-01-20
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    相关资源
    最近更新 更多