如何使用PHPQuery删除HTML标记?

时间:2011-01-10 16:11:33

标签: html phpquery

Update1:​​使用完整的源代码:

$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我不知道它是否已经解决。

有关如何删除&lt; 脚本&gt;的任何线索来自这个HTML?

最诚挚的问候,


您好,

我需要删除所有&lt; 脚本&gt;使用PhpQuery从HTML文档中标记。

我做了以下事情:

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

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

它不会删除&lt; 脚本&gt;标签和内容。可以用PhpQuery做到这一点吗?

最诚挚的问候,

3 个答案:

答案 0 :(得分:10)

这有效:

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

这不起作用:

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

答案 1 :(得分:6)

从文档看起来你会这样做:

$doc->remove('script');

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

编辑:

看起来PHPQuery中存在一个错误,但这可以改为:

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

答案 2 :(得分:1)

我希望像这样简单的东西能起作用 PQ( 'TD [列跨度= “2”]') - &GT;清除( '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
相关问题