如何使用 PHPQuery 删除 HTML 标签?

发布于 2024-10-11 09:51:48 字数 995 浏览 4 评论 0原文

更新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="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 可以做到这一点吗?

此致,

Update1: With the full source code:

$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;

The source code is this the above. I have also read that exists a bug, http://code.google.com/p/phpquery/issues/detail?id=150 I don't know if it is solved.

Any clues on how to remove the <script> from this HTML?

Best Regards,


Hi,

I need to remove all <script> tags from a HTML document using PhpQuery.

I have done the following:

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

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

It is not removing the <script> tags and contents. It is possible to do this with PhpQuery?

Best Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍白女子 2024-10-18 09:51:48

这有效:

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

这不起作用:

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

This works:

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

This doesn't work:

$html = $html->find('script')->remove();
echo $html;
勿忘心安 2024-10-18 09:51:48

从文档来看,您似乎会这样做:

$doc->remove('script');

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

编辑:

看起来 PHPQuery 中有一个错误,这有效:

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

From the documentation it looks like you would do this:

$doc->remove('script');

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

EDIT:

Looks like there's a bug in PHPQuery, this works instead:

$doc->find('script')->remove();
过期以后 2024-10-18 09:51:48

我希望像这样简单的事情会起作用
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

I was hoping something simple like this would work
pq('td[colspan="2"]')->remove('b');
Unfortunately it did not work as I hoped.
I ran across this stackoverflow and tried what was mentioned without success.

This is what worked for me.

$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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文