Regular expression to remove an iframe

冷暖自知 提交于 2019-12-05 23:19:57
<?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);
?>
user4035

str_replace doesn't use regexps. Use this instead:

$str = preg_replace('/<iframe.*?>/', '', $str);

This will replace

<iframe something>

to empty string

str_replace() does not support regular expressions. You are after preg_replace(). You will want a regex similar to:

$html = preg_replace('/<iframe>.*<\/iframe>/is', '', $html);

Although, as this is HTML you should avoid regexing and use an HTML parser instead.

Adding to the last answer this is a more efficient regular expression for replacing multiple iframes.

$html = preg_replace('/(<iframe.+">)/is', '', $html);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!