代码之家  ›  专栏  ›  技术社区  ›  rockstardev

PHP:从HTML字符串中去掉一个特定的标记?

php
  •  21
  • rockstardev  · 技术社区  · 15 年前

    <html>
     <body>
     bla bla bla bla
      <div id="myDiv"> 
             more text
          <div id="anotherDiv">
               And even more text
          </div>
      </div>
    
      bla bla bla
     </body>
    </html>
    

    <div id="anotherDiv"> 直到它结束 <div>

    8 回复  |  直到 5 年前
        1
  •  34
  •   Gordon Haim Evgi    15 年前

    native DOM

    $dom = new DOMDocument;
    $dom->loadHTML($htmlString);
    $xPath = new DOMXPath($dom);
    $nodes = $xPath->query('//*[@id="anotherDiv"]');
    if($nodes->item(0)) {
        $nodes->item(0)->parentNode->removeChild($nodes->item(0));
    }
    echo $dom->saveHTML();
    
        2
  •  14
  •   Florent Joice    13 年前

    你可以用 preg_replace() 比如:

    $string = preg_replace('/<div id="someid"[^>]+\>/i', "", $string);
    
        3
  •  5
  •   RafaSashi    5 年前

    使用本机 XML Manipulation Library

    假设您的html内容存储在变量$html中:

    $html='<html>
     <body>
     bla bla bla bla
      <div id="myDiv"> 
             more text
          <div id="anotherDiv">
               And even more text
          </div>
      </div>
    
      bla bla bla
     </body>
    </html>';
    

    要按ID删除标记,请使用以下代码:

        $dom=new DOMDocument;
    
        $dom->validateOnParse = false;
    
        $dom->loadHTML( $html );
    
        // get the tag
    
        $div = $dom->getElementById('anotherDiv');
    
       // delete the tag
    
        if( $div && $div->nodeType==XML_ELEMENT_NODE ){
    
            $div->parentNode->removeChild( $div );
        }
    
        echo $dom->saveHTML();
    

    请注意 libxml doctype 为了使用 getElementById

    在这种情况下,可以使用 <!doctype>

    $html = '<!doctype>' . $html;
    

    DOMXPath 要使用xpath查找元素,请执行以下操作:

    $dom=new DOMDocument;
    
    $dom->validateOnParse = false;
    
    $dom->loadHTML( $html );
    
    $xp=new DOMXPath( $dom );
    
    $col = $xp->query( '//div[ @id="anotherDiv" ]' );
    
    if( !empty( $col ) ){
    
        foreach( $col as $node ){
    
            $node->parentNode->removeChild( $node );
    
        }
    
    }
    
    echo $dom->saveHTML();
    

    第一种方法不考虑标记。如果您想使用具有相同id但不同标记的第二个方法,那么 form ,只需替换 //div 在里面 //div[ @id="anotherDiv" ] 由' //form '

        4
  •  0
  •   ItsPronounced Finn    15 年前

    strip_tags()函数就是您要找的。

    http://us.php.net/manual/en/function.strip-tags.php

        5
  •  -1
  •   Aram Kocharyan    9 年前

    我写这些来剥离特定的标签和属性。因为它们是regex,所以不能100%保证在所有情况下都能工作,但这对我来说是一个公平的权衡:

    // Strips only the given tags in the given HTML string.
    function strip_tags_blacklist($html, $tags) {
        foreach ($tags as $tag) {
            $regex = '#<\s*' . $tag . '[^>]*>.*?<\s*/\s*'. $tag . '>#msi';
            $html = preg_replace($regex, '', $html);
        }
        return $html;
    }
    
    // Strips the given attributes found in the given HTML string.
    function strip_attributes($html, $atts) {
        foreach ($atts as $att) {
            $regex = '#\b' . $att . '\b(\s*=\s*[\'"][^\'"]*[\'"])?(?=[^<]*>)#msi';
            $html = preg_replace($regex, '', $html);
        }
        return $html;
    }
    
        6
  •  -1
  •   Community CDub    8 年前

    这个怎么样?

    // Strips only the given tags in the given HTML string.
    function strip_tags_blacklist($html, $tags) {
        $html = preg_replace('/<'. $tags .'\b[^>]*>(.*?)<\/'. $tags .'>/is', "", $html);
        return $html;
    }
    
        7
  •  -1
  •   Jonathan Land    6 年前

    preg_replace() ,以下是一个适用于单个标记或标记数组的版本:

    /**
     * @param $str string
     * @param $tags string | array
     * @return string
     */
    
    function strip_specific_tags ($str, $tags) {
      if (!is_array($tags)) { $tags = array($tags); }
    
      foreach ($tags as $tag) {
        $_str = preg_replace('/<\/' . $tag . '>/i', '', $str);
        if ($_str != $str) {
          $str = preg_replace('/<' . $tag . '[^>]*>/i', '', $_str);
        }
      }
      return $str;
    }
    
    推荐文章