2011-01-10 106 views
5

UPDATE1:随着完整的源代码:如何使用PHPQuery去除HTML标签?

$html1 = '<div class="pubanunciomrec" style="background:#FFFFFF;"><script type="text/javascript"><!-- 
google_ad_slot = "9853257829"; 
google_ad_width = 300; 
google_ad_height = 250; 
//--> 
</script> 
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script></div>'; 

$doc = phpQuery::newDocument($html1); 
$html1 = $doc->remove('script'); 
echo $html1; 

的源代码本以上。我也读过,存在一个错误,http://code.google.com/p/phpquery/issues/detail?id=150我不知道它是否解决了。

有关如何从此HTML中删除< 脚本>的任何线索?

最好的问候,


嗨,

我需要从使用PhpQuery一个HTML文档中删除所有< 脚本>标记。

我也做了以下内容:

$doc = phpQuery::newDocument($html); 

$html = $doc['script']->remove(); 
echo $html; 

它不删除< 脚本>标记和内容。使用PhpQuery可以做到这一点吗?

最好的问候,

回答

10

这工作:

$html->find('script')->remove(); 
echo $html; 

这不起作用:

$html = $html->find('script')->remove(); 
echo $html; 
6

从它看起来像你这样做的文档:

$doc->remove('script'); 

http://code.google.com/p/phpquery/wiki/Manipulation#Removing

编辑:

看起来像有在PHPQuery一个bug,这个作品相反:

$doc->find('script')->remove(); 
+0

您好,感谢您的答复。它不工作。还有什么线索?最好的问候, – 2011-01-10 17:27:48

1

我希望这样简单的工作 pq('td [colspan =“2”]') - > remove('b'); 不幸的是,它并没有如我所愿。 我遇到了这个stackoverflow,并试图提到没有成功。

这是我工作的。

$doc = phpQuery::newDocumentHTML($html); 
// used newDocumentHTML and stored it's return into $doc 

$doc['td[colspan="2"] b']->remove(); 
// Used the $doc var to call remove() on the elements I did not want from the DOM 
// In this instance I wanted to remove all bold text from the td with a colspan of 2 

$d = pq('td[colspan="2"]'); 
// Created my selection from the current DOM which has the elements removed earlier 

echo pq($d)->text(); 
// Rewrap $d into PHPquery and call what ever function you want