【问题标题】:How to remove an HTML tag with PHPQuery?如何使用 PHPQuery 删除 HTML 标签?
【发布时间】:2011-01-10 16:11:32
【问题描述】:

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;

源代码就是上面这个。我也看过存在bug,http://code.google.com/p/phpquery/issues/detail?id=150不知道有没有解决。

关于如何从这个 HTML 中删除 script> 的任何线索?

最好的问候,


嗨,

我需要使用 PhpQuery 从 HTML 文档中删除所有 script> 标记。

我做了以下事情:

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

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

它不会删除 script> 标签和内容。可以用 PhpQuery 做到这一点吗?

最好的问候,

【问题讨论】:

    标签: html phpquery


    【解决方案1】:

    这行得通:

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

    这不起作用:

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

    【讨论】:

      【解决方案2】:

      从文档看来你会这样做:

      $doc->remove('script');
      

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

      编辑:

      看起来 PHPQuery 中存在错误,这可以代替:

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

      【讨论】:

      • 您好,感谢您的回复。它不工作。还有更多线索吗?最好的问候,
      • 您应该提供更多信息供人们测试。你能提供 HTML 吗?
      • 看起来有一个错误,我已经更新了我的答案以反映这一点。
      【解决方案3】:

      我希望像这样简单的事情会起作用 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-23
        • 2017-04-21
        • 2019-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-21
        相关资源
        最近更新 更多