How to remove HTML tag using PHPQuery?

Update1: with 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 above. I also read that there is an error, http://code.google.com/p/phpquery/issues/detail?id=150 I do not know if this is allowed.

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

Regards,


Hi,

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

I have done the following:

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

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

It does not remove tags and <script> content. Can this be done using PhpQuery?

Regards,

+5
source share
3 answers

It works:

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

This does not work:

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

From the documentation, it looks like you will do the following:

$doc->remove('script');

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

EDIT:

, PHPQuery, :

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

I was hoping that something simple, how it would work rd ('td [Column merge = "2"]') → delete ('B'); Unfortunately, this did not work as I hoped. I stumbled upon this stackoverflow and tried what was mentioned without success.

This is what worked for me.

$doc = phpQuery::newDocumentHTML($html); 
// used newDocumentHTML and stored it 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
source

All Articles