2012-08-17 112 views
-1

可能重复:
Error when remove a iframe but can't remove tag p如何删除文本中的iframe src?

我有一个示例代码:

$content = '<p><iframe width="640" scrolling="no" height="150" frameborder="1" marginheight="0" marginwidth="0" src="http://test.html"></iframe></p><p>Hello world</p>'; 
$text = preg_replace('/<p>\s*(<iframe src="http://test.html" .*>*.<\/iframe>)\s*<\/p>/iU', '', $content); 
echo $text; 

如何修复删除此iframe的结果是:<p>Hello world</p>

+0

正则表达式,你应该避免这样的任务。 DOM函数的遍历前端将是明智的。以QueryPath为例:'if(iframe [src = http://test.html]“) - > remove() - > top() - > html()' – mario 2012-08-17 03:16:40

+0

'iframe '$ content'中的''没有关闭。如果不是拼写错误,我认为正则表达式可能会更好......'/

\ s *

/'' – Passerby 2012-08-17 03:37:24

回答

0
$text = preg_replace('|<iframe [^>]*(src="http://test.html")[^>]*|', '', $content);