0

好的,因为它是 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); ?>
4

4 回答 4

9

我不会使用正则表达式。相反,我会使用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>
于 2012-05-18T19:42:16.873 回答
6
<?php $content = preg_replace('/<div class="sometestclass">.*?<\/div><!-- END: .sometestclass -->/s','',$content); ?>

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

此外,这种模式不会找到div带有 class 的嵌入元素sometestclass。你需要递归。

于 2012-05-18T20:33:05.763 回答
0

只是一些 CSS.sometestclass{display: none;}怎么样?

于 2012-05-18T19:44:42.583 回答
0

对于 UTF-8 问题,我在PHP 手册中找到了一个 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-07-31T12:40:49.233 回答