【发布时间】:2017-04-27 16:18:06
【问题描述】:
我需要在我的网站上加载一些第 3 方小部件。他们分发它的唯一方法是通过笨拙的旧<iframe>。
我没有太多选择,所以我要做的是获取 iframe html 代码,使用我网站上的代理页面,如下所示:
$iframe = file_get_contents('http://example.com/page_with_iframe_html.php');
然后我必须像这样删除 iframe 中的一些特定部分:
$iframe = preg_replace('~<div class="someclass">[\s\S]*<\/div>~ix', '', $iframe);
通过这种方式,我打算删除不需要的部分。最后我只是像这样输出 iframe:
echo ($iframe);
iframe 可以正常输出,但不需要的部分仍然存在。正则表达式本身使用 regex101 进行了测试,但它不起作用。
【问题讨论】:
-
使用
DOMDocument来解析HTML的内容,而不是正则表达式。 -
您能分享您的
HTML内容吗? -
@SahilGulati 不是真的,但这里是等价的
<div class="someclass"> <span class="hot-line-text"> hotline: </span> <a id="hot-line-tel" class="hot-line-link" href="tel:0000" target="_parent"> <button class="hot-line-button"></button> <span class="hot-line-number">0000</span> </a> </div> -
你的预期输出是什么?
-
@SahilGulati 抱歉,上面的代码是我需要删除的代码。输出代码要大得多。我需要做的就是从输出的其余部分中删除我之前评论中的代码
标签: php regex iframe preg-replace domdocument