用于删除iframe的正则表达式 [英] Regular expression to remove an iframe

查看:1033
本文介绍了用于删除iframe的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 str_replace 将所有iframe替换为我自己的内容。我相信我可以使用正则表达式但不完全确定如何删除iframe标记中的所有内容。

I'm trying to use str_replace to replace all iframes with my own content. I believe I can use regular expression but not exactly sure how to remove all content within the iframe tag.

由于我不确定如何实现这一点,下面是我一直在使用什么。但它只是替换iframe,所以当一个页面被加载到iframe加载的任何地方时,现在只显示如果iframe要正确解析就会加载的URL。

Since I'm not really sure how to achieve this, below is what I have been using. But it's only replacing "iframe" with nothing so when a page is loaded anywhere an iframe would load now just shows the URL that would have been loaded if the iframe were to parse properly.

$toremove = str_replace("iframe", "", $toremove);

以下是使用上述代码时加载的内容而不是iframe的示例输出。你可以看到它是整个iframe,只是没有iframe这个词打破iframe(这比显示它更好)

Here is an example output of what gets loaded instead of the iframe when using the code above. You can see that it's the whole iframe just without the word "iframe" which is breaking the iframe (which is better then displaying it)

< src="http://cdn2.adexprt.com/exo_na/center.html" width="728" height="90" frameborder="0" scrolling="no"> 

但现在我想完全删除iframe及其所有内容,以便我可以将自己的内容放入它没有显示旧的破坏代码的地方。但是我不确定如何使用正则表达式编辑我的 str_replace 而不仅仅是工作iframe

But now I want to completely remove the iframe and all it's content so I can place my own content in it's place without showing the old broken code as well. But I'm not sure how to edit my str_replace with a regular expression and not just the work "iframe"

推荐答案

<?php
$iframe='<iframe src="http://cdn2.adexprt.com/exo_na/center.html" width="728" height="90" frameborder="0" scrolling="no">
</iframe>';
$pattern = "#<iframe[^>]+>.*?</iframe>#is";
echo preg_replace($pattern, "", $iframe);
?>

这篇关于用于删除iframe的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆