Как удалить тег HTML с помощью PHPQuery? - PullRequest
5 голосов
/ 10 января 2011

Update1: с полным исходным кодом:

$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 Я не знаю, если она решена.

Любые подсказки о том, как удалить <<em> script > из этого HTML?

С наилучшими пожеланиями,


Привет,

Мне нужно удалить все теги <<em> script > из документа HTML с помощью PhpQuery.

Я сделал следующее:

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

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

Он не удаляет теги и содержимое <<em> script >.Это можно сделать с помощью PhpQuery?

С наилучшими пожеланиями,

Ответы [ 3 ]

10 голосов
/ 06 июня 2012

Это работает:

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

Это не работает:

$html = $html->find('script')->remove();
echo $html;
6 голосов
/ 10 января 2011

Из документации похоже, что вы бы сделали это:

$doc->remove('script');

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

РЕДАКТИРОВАТЬ:

Похоже, что в PHPQuery есть ошибка, это работаетвместо:

$doc->find('script')->remove();
1 голос
/ 25 сентября 2011

Я надеялся, что что-то простое сработает рд ( 'тд [Объединение колонок = "2"]') -> удалить ( 'B'); К сожалению, это не сработало, как я надеялся. Я наткнулся на этот стекопоток и безуспешно попробовал то, что было сказано.

Вот что у меня сработало.

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