【问题标题】:Php Remove content html from specific classphp 从特定类中删除内容 html
【发布时间】:2021-08-12 08:15:14
【问题描述】:

您好,我想从父 id 或类中删除所有 html 代码

<?php
    $html = '<div class="m-interstitial"><div class="m-interstitial">
<div class="m-interstitial__ad" data-readmore-target="">
<div class="m-block-ad" data-tms-ad-type="box" data-tms-ad-status="idle" data-tms-ad-pos="1">
<div class="m-block-ad__label m-block-ad__label--report-enabled"><span class="m-block-ad__label__text">Advertising</span> <button class="m-block-ad__label__report-link" title="Report this ad" data-tms-ad-report=""> </button></div>
<div class="m-block-ad__content">&nbsp;</div>
</div>
</div>
<button class="m-interstitial__unlock-btn" data-readmore-unlocker=""> <span class="m-interstitial__unlock-btn__text">Read more</span>
</button></div>';


// I tried it with below code but it does not work

//$remove = preg_replace('#<div class="m-interstitial">(.*?)</div>#', '', $html); 
$remove = preg_replace('#<div class="m-interstitial">(.*?)</div>#s', '', $html);
var_dump($remove); // result = normally I want the result is empty "" but it seems does not works.

我的 preg_replace 不能如我所愿。有什么想法吗?

谢谢

【问题讨论】:

  • &lt;script&gt; 在里面做什么?有多个带有class="m-interstitial" 的 div,您要删除哪一个?该 HTML 中没有元素 &lt;m-interstitial"&gt;,只有 &lt;div class="m-interstitial"&gt;$fullcontent 首先包含什么,那是您的 HTML 字符串吗?
  • 对不起,我刚刚编辑了。我第一次使用 Stackoverflow。有时间可以帮我看看。谢谢
  • 虽然您可以使用正则表达式来操作 html,但不要!而是使用像domdocument3v4l.org/ZBmDZ这样的html解析器

标签: php


【解决方案1】:

根据您的代码示例,您为什么不直接设置 $html = '';如果那是你想要的?如果您有不同的 HTML,则使用 XPath 查找匹配项:

<?php
$html = '<div class="m-interstitial">
    <div class="m-interstitial">
        <div class="m-interstitial__ad" data-readmore-target="">
            <div class="m-block-ad" data-tms-ad-type="box" data-tms-ad-status="idle" data-tms-ad-pos="1">
                <div class="m-block-ad__label m-block-ad__label--report-enabled"><span class="m-block-ad__label__text">Advertising</span> <button class="m-block-ad__label__report-link" title="Report this ad" data-tms-ad-report=""> </button></div>
                <div class="m-block-ad__content">&nbsp;</div>
            </div>
        </div>
        <button class="m-interstitial__unlock-btn" data-readmore-unlocker=""> <span class="m-interstitial__unlock-btn__text">Read more</span></button>
    </div>';

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->omitXmlDeclaration = true;
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = false;
$dom->strictErrorChecking = false;
$dom->formatOutput = false;
$dom->loadHTML('<?xml encoding="utf-8" ?>'.$html);
libxml_clear_errors();
libxml_use_internal_errors(false);

$xpath = new DOMXPath($dom);
$child = $xpath->query("(//div[@class='m-interstitial'])[1]");
$parent = $child[0]->parentNode;
$parent->removeChild($child[0]);
echo $dom->saveXML($dom->documentElement);

我不能 100% 确定这是否是您想要做的,但理论上,使用 XPath/DOM 会像这样使用。

导致 HTML 为空(因为您想过滤掉 html 的父元素或根元素)。

<html><body/></html>

【讨论】:

  • 因为在代码之前和之后可以有一些另外的html代码,所以我回复你了。谢谢你。很好的答案
【解决方案2】:

我只是做几乎相同的事情,但你似乎更好

    $doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$styles = $xpath->query('//div[@class="m-interstitial"]');
if ($styles) {
  foreach ($styles as $style) {
    $style->textContent = "";
  }
}
$html = $doc->saveHTML();


var_dump($html );

【讨论】:

  • 我的代码 sn-p 中的额外检查和配置旨在防止 HTML5 的一些常见问题(与 XML 或过时的 XHTML 标准不同——HTML5 并不严格但非常“松散”)。如果您传递无效/不完整或错误的 HTML,这也可以防止 DOMDocument 引发错误。要删除 &lt;style&gt; 你也可以这样做 $style-&gt;parentNode-&gt;removeChild($style);
  • 谢谢你,很好的提示
猜你喜欢
  • 2021-10-09
  • 2013-05-01
  • 2015-11-14
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多