【发布时间】: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"> </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 不能如我所愿。有什么想法吗?
谢谢
【问题讨论】:
-
<script>在里面做什么?有多个带有class="m-interstitial"的 div,您要删除哪一个?该 HTML 中没有元素<m-interstitial">,只有<div class="m-interstitial">。$fullcontent首先包含什么,那是您的 HTML 字符串吗? -
对不起,我刚刚编辑了。我第一次使用 Stackoverflow。有时间可以帮我看看。谢谢
-
虽然您可以使用正则表达式来操作 html,但不要!而是使用像domdocument3v4l.org/ZBmDZ这样的html解析器
标签: php