php 如何使用标签的 id 去除标签及其所有内部 html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3308530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:17:51  来源:igfitidea点击:

How to strip a tag and all of its inner html using the tag's id?

phphtmlreplacetagsdomparser

提问by coderama

I have the following 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>

I want to remove everything starting from <div id="anotherDiv">until its closing <div>. How do I do that?

我想删除从开始<div id="anotherDiv">到结束的所有内容<div>。我怎么做?

回答by Gordon

With native DOM

使用原生 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();

回答by Haim Evgi

You can use preg_replace()like:

你可以使用preg_replace()像:

$string = preg_replace('/<div id="someid"[^>]+\>/i', "", $string);

回答by RafaSashi

Using the native XML Manipulation Library

使用原生XML 操作库

Assuming that your html content is stored in the variable $html:

假设您的 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>';

To delete the tag by ID use the following code:

要按 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();

Note that certain versions of libxmlrequire a doctypeto be present in order to use the getElementByIdmethod.

请注意,某些版本libxml需要doctype存在a才能使用该getElementById方法。

In that case you can prepend $html with <!doctype>

在这种情况下,您可以在 $html 前面加上 <!doctype>

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

Alternatively, as suggested by Gordon's answer, you can use DOMXPathto find the element using the xpath:

或者,正如戈登的回答所建议的那样,您可以使用DOMXPathxpath 来查找元素:

$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();

The first method works regardless the tag. If you want to use the second method with the same id but a different tag, let say form, simply replace //divin //div[ @id="anotherDiv" ]by '//form'

无论标签如何,第一种方法都有效。如果您想使用具有相同 id 但不同标签的第二种方法,例如form,只需将//divin替换//div[ @id="anotherDiv" ]为 ' //form'

回答by ItsPronounced

strip_tags() function is what you are looking for.

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

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

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

回答by Aram Kocharyan

I wrote these to strip specific tags and attributes. Since they're regex they're not 100% guaranteed to work in all cases, but it was a fair tradeoff for me:

我写这些是为了去除特定的标签和属性。由于它们是正则表达式,因此不能 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;
}

回答by Hoàng V? Tgtt

how about this?

这个怎么样?

// 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;
}

回答by Jonathan Land

Following RafaSashi's answer using preg_replace(), here's a version that works for a single tag or an array of tags:

按照 RafaSashi 的回答使用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;
}