2011-01-10 105 views
5

UPDATE1:隨着完整的源代碼:如何使用PHPQuery去除HTML標籤?

$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可以做到這一點嗎?

最好的問候,

回答

10

這工作:

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

這不起作用:

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

從它看起來像你這樣做的文檔:

$doc->remove('script'); 

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

編輯:

看起來像有在PHPQuery一個bug,這個作品相反:

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

您好,感謝您的答覆。它不工作。還有什麼線索?最好的問候, – 2011-01-10 17:27:48

1

我希望這樣簡單的工作 pq('td [colspan =「2」]') - > remove('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