如何使用PHPQuery删除HTML标记?
更新1:使用完整的源代码:如何使用PHPQuery删除HTML标记?,html,phpquery,Html,Phpquery,更新1:使用完整的源代码: $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
$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;
它不会删除标签和内容。是否可以使用PhpQuery执行此操作
致以最诚挚的问候,从文档中可以看出,您可能会这样做:
$doc->remove('script');
编辑:
看起来PHPQuery中有一个bug,它可以工作:
$doc->find('script')->remove();
我希望像这样简单的事情能奏效 pq('td[colspan=“2”]”)->删除('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
这项工作:
$html->find('script')->remove();
echo $html;
这不起作用:
$html = $html->find('script')->remove();
echo $html;
嗨,谢谢你的回复。它不起作用。还有什么线索吗?致以最诚挚的问候,您应该提供更多信息供人们测试。你能提供HTML吗?看起来有个bug,我已经更新了我的答案来反映这一点。
$html = $html->find('script')->remove();
echo $html;