首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHPQuery删除HTML标记?

如何使用PHPQuery删除HTML标记?
EN

Stack Overflow用户
提问于 2011-01-10 16:11:32
回答 3查看 7.8K关注 0票数 5

Update1:具有完整的源代码:

代码语言:javascript
复制
$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;

源代码就是上面提到的。我也读过一个bug,http://code.google.com/p/phpquery/issues/detail?id=150我不知道它是否解决了。

关于如何从这个HTML中删除,有什么线索吗?</div><div>诚挚的问候,</div><div></div><div>嗨,</div><div>我需要使用<script>从PhpQuery文档中删除所有的PhpQuery标记。</div><div>我做了以下工作:</div><pre><code>$doc = phpQuery::newDocument($html); $html = $doc['script']-&gt;remove(); echo $html;</code></pre><div>它不是删除<script>标记和内容。用PhpQuery可以做到这一点吗?</div><div>诚挚的问候,</div><div></div>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-06 07:36:58

这样做是可行的:

代码语言:javascript
复制
$html->find('script')->remove();
echo $html;

这不管用:

代码语言:javascript
复制
$html = $html->find('script')->remove();
echo $html;
票数 10
EN

Stack Overflow用户

发布于 2011-01-10 16:59:14

从文档来看,您可能会这样做:

代码语言:javascript
复制
$doc->remove('script');

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

编辑:

看起来在PHPQuery中有一个bug,但是这是可行的:

代码语言:javascript
复制
$doc->find('script')->remove();
票数 6
EN

Stack Overflow用户

发布于 2011-09-25 10:16:03

我希望像这样简单的东西能工作pq(‘tdcolspan=“2’)->remove(‘b’);不幸的是它没有像我希望的那样工作。我遇到了这种堆叠溢出,并尝试了提到的,但没有成功。

,这就是对我有用的东西,

代码语言:javascript
复制
$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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4648819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档