亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 DOMDocument 將特定的順序元素包裝在單個容器中?

如何使用 DOMDocument 將特定的順序元素包裝在單個容器中?

PHP
慕標5832272 2023-03-11 13:26:36
假設我有以下標記:<h3 class="handorgel__header">    <button class="handorgel__header__button">        Books, Literature and Languages    </button></h3><div class="handorgel__content">    <div class="handorgel__content__inner">        <p>Hello</p>    </div></div><h3 class="handorgel__header">    <button class="handorgel__header__button">        Business and Consumer Information    </button></h3><div class="handorgel__content">    <div class="handorgel__content__inner">        <p>World</p>    </div></div><p>Some text in between</p><h3 class="handorgel__header">    <button class="handorgel__header__button">        Fine Arts and Music    </button></h3><div class="handorgel__content">    <div class="handorgel__content__inner">        <p>Hello</p>    </div></div><h3 class="handorgel__header">    <button class="handorgel__header__button">        Genealogy    </button></h3><div class="handorgel__content">    <div class="handorgel__content__inner">        <p>World</p>    </div></div>我想包裝每組.handorgel__*元素,以便它們包含在一個容器中<div class="handorgel">。<div class="handorgel">    <h3 class="handorgel__header">        <button class="handorgel__header__button">            Books, Literature and Languages        </button>    </h3>    <div class="handorgel__content">        <div class="handorgel__content__inner">            <p>Hello</p>        </div>    </div>    <h3 class="handorgel__header">        <button class="handorgel__header__button">            Business and Consumer Information        </button>    </h3>    <div class="handorgel__content">        <div class="handorgel__content__inner">            <p>World</p>        </div>    </div></div><p>Some text in between</p><div class="handorgel">    <h3 class="handorgel__header">        <button class="handorgel__header__button">            Fine Arts and Music        </button>    </h3>每個組中可以有任意數量的元素,以及頁面的任意數量的組。我怎樣才能檢測到這些組并適當地包裝它們?我目前DOMDocument在這個項目中使用了很多東西,所以如果可能的話,我也想將它用于這個目的,除非有明顯更好的方法。
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

經過一系列的反復試驗,我自己設法讓這個工作起來。并不像我想象的那么困難,DOMDocument 實際上負責一些刪除邏輯本身。



/**

 * Wrap handorgel groups in appropriate containers

 *

 * @param string $content

 *

 * @return string

 */

function gpl_wrap_handorgel_shortcodes(string $content): string {

    if (! is_admin() && $content) {

        $DOM = new DOMDocument();


        // disable errors to get around HTML5 warnings...

        libxml_use_internal_errors(true);


        // load in content

        $DOM->loadHTML(mb_convert_encoding("<html><body>{$content}</body></html>", "HTML-ENTITIES", "UTF-8"), LIBXML_HTML_NODEFDTD);


        // reset errors to get around HTML5 warnings...

        libxml_clear_errors();


        $body = $DOM->getElementsByTagName("body");


        $handorgels = [];


        $prev_class = "";


        foreach ($body[0]->childNodes as $element) {

            /**

             * Ensure that only HTML nodes get checked/modified

             */

            if ($element->nodeType == 1) {

                $current_class = $element->getAttribute("class");


                /**

                 * Find any handorgel elements

                 */

                if (preg_match("/handorgel__/", $current_class)) {

                    $group = array_key_last($handorgels);


                    /**

                     * If the previous class didn't include `handorgel__`, create a new handorgel object

                     */

                    if (! preg_match("/handorgel__/", $prev_class)) {

                        $handorgels[] = [

                            "container" => $DOM->createElement("div"),

                            "elements"  => [],

                        ];


                        /**

                         * Update `$group` to match the new container

                         */

                        $group = array_key_last($handorgels);

                    }


                    /**

                     * Append the current element to the group to be moved after all sequential handorgel

                     * elements are located for its group

                     */

                    $handorgels[$group]["elements"][] = $element;

                }


                /**

                 * Update `$prev_class` to track where handorgel groups should begin and end

                 */

                $prev_class = $current_class;

            }

        }


        /**

         * Construct the grouped handorgels

         */

        if ($handorgels) {

            foreach ($handorgels as $group => $handorgel) {

                $handorgel["container"]->setAttribute("class", "handorgel");


                foreach ($handorgel["elements"] as $key => $element) {

                    /**

                     * Insert the container in the starting position for the group

                     */

                    if ($key === 0) {

                        $element->parentNode->insertBefore($handorgels[$group]["container"], $element);

                    }


                    $handorgel["container"]->appendChild($element);

                }

            }

        }


        /**

         * Remove unneeded tags (inserted for parsing reasons)

         */

        $content = remove_extra_tags($DOM); // custom function that removes html/body and outputs the DOMDocument as a string

    }


    return $content;

}

add_filter("the_content", "gpl_wrap_handorgel_shortcodes", 30, 1);


查看完整回答
反對 回復 2023-03-11
  • 1 回答
  • 0 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號