【问题标题】:How to remove entire div with preg_replace如何使用 preg_replace 删除整个 div
【发布时间】:2012-05-18 19:39:55
【问题描述】:

好的,因为这是 WordPress 的问题,遗憾的是它更深了一点,我需要删除父 div 及其内部的每个表示:

<div class="sometestclass">
   <img ....>
   <div>.....</div>
   any other html tags
</div><!-- END: .sometestclass -->

我唯一的想法是匹配以下开头的所有内容:

<div class="sometestclass">

并以:

结尾
<!-- END: .sometestclass -->

之间的所有内容(我可以随意标记父 div 的结尾,这只是一个示例)。 任何人都知道如何做到这一点:

<?php $content = preg_replace('?????','',$content); ?>

【问题讨论】:

标签: php html html-parsing


【解决方案1】:

我不会使用正则表达式。相反,我会使用 DOMDocument 类。只需找到该类的所有 div 元素,然后将它们从其父级中删除即可:

$html = "<p>Hello World</p>
         <div class='sometestclass'>
           <img src='foo.png'/>
           <div>Bar</div>
         </div>";

$dom = new DOMDocument;
$dom->loadHTML( $html );

$xpath = new DOMXPath( $dom );
$pDivs = $xpath->query(".//div[@class='sometestclass']");

foreach ( $pDivs as $div ) {
  $div->parentNode->removeChild( $div );
}

echo preg_replace( "/.*<body>(.*)<\/body>.*/s", "$1", $dom->saveHTML() );

结果:

<p>Hello World</p>

【讨论】:

  • 遗憾的是我对此一无所知:(。你能用某种样本解释一下吗?
  • 他可以使用 XPath 查询直接找到具有正确类的 div,而不是循环遍历所有 div。
  • @JonathanSampson 这个例子给了我一个关于如何做的想法,但这取决于一个父(未命名)div。但是,如果我没有那个父 div,并且只需要完成

    Hello World

    怎么办?
  • 所以输入的html是:

    Hello World

    Bar
  • @Arash $1 指的是正则表达式中的(.*)
【解决方案2】:
<?php $content = preg_replace('/<div class="sometestclass">.*?<\/div><!-- END: .sometestclass -->/s','',$content); ?>

我的 RegEx 有点生疏,但我认为这应该可以。请注意,正如其他人所说,RegEx 没有适当地处理 HTML 的某些复杂性。

此外,此模式不会找到类 sometestclass 的嵌入 div 元素。你需要递归。

【讨论】:

  • 这可能有效,但我已经使用了 DOMDocument 方法。但是感谢 RegEx,它更容易在示例中学习 :)
【解决方案3】:

来点 CSS .sometestclass{display: none;} 怎么样?

【讨论】:

    【解决方案4】:

    对于 UTF-8 问题,我在 PHP-manual 找到了一个 hack

    所以我的函数如下所示:

    function rem_fi_cat() {
    /* This function removes images from _within_ the article.
     * If these images are enclosed in a "wp-caption" div-tag.
     * If the articles are post formatted as "image".
     * Only on home-page, front-page an in category/archive-pages.
     */
    if ( (is_home() || is_front_page() || is_category()) && has_post_format( 'image' ) ) {
        $document = new DOMDocument();
        $content = get_the_content( '', true );
        if( '' != $content ) {
            /* incl. UTF-8 "hack" as described at 
             * http://www.php.net/manual/en/domdocument.loadhtml.php#95251
             */
            $document->loadHTML( '<?xml encoding="UTF-8">' . $content );
            foreach ($doc->childNodes as $item) {
                if ($item->nodeType == XML_PI_NODE) {
                    $doc->removeChild($item); // remove hack
                    $doc->encoding = 'UTF-8'; // insert proper
                }
            }
            $xpath = new DOMXPath( $document );
            $pDivs = $xpath->query(".//div[@class='wp-caption']");
    
            foreach ( $pDivs as $div ) {
                $div->parentNode->removeChild( $div );
            }
    
            echo preg_replace( "/.*<div class=\"entry-container\">(.*)<\/div>.*/s", "$1", $document->saveHTML() );
    
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2013-01-10
      • 1970-01-01
      相关资源
      最近更新 更多