开发者

How to remove an HTML tag with PHPQuery?

开发者 https://www.devze.com 2023-02-04 12:09 出处:网络
Update1: With the full source code: $html1 = \'<div class=\"pubanunciomrec\" style=\"background:#FFFFFF;\"><script type=\"text/javascript\"><!--

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/jav开发者_如何学Goascript"
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,


This works:

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

This doesn't work:

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


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();


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号