André
André

Reputation: 25554

How to remove an HTML tag with 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,

Upvotes: 5

Views: 8146

Answers (3)

Mat Weir
Mat Weir

Reputation: 116

This works:

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

This doesn't work:

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

Upvotes: 10

CTS_AE
CTS_AE

Reputation: 14823

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

Upvotes: 1

Ryan Doherty
Ryan Doherty

Reputation: 38740

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

Upvotes: 6

Related Questions