【问题标题】:PHP simple dom parser select and remove multiple tags at oncePHP简单的dom解析器一次选择并删除多个标签
【发布时间】:2012-02-23 07:31:48
【问题描述】:

我正在使用 PHP 简单的 dom 解析器 http://simplehtmldom.sourceforge.net/manual.htm。 我可以通过

删除具有特定 id 的标签
$html = str_get_html('<div><div class="two">two</div></div>');     
$e = $html ->find('.two',0);
$e->outertext = '';
echo $html ->outertext;

阅读简单的 dom api 文档,我看到我们可以通过在 find 函数中包含多个类或 id 来一次选择多个标签,但是我们如何一次删除所有标签。

我尝试了以下方法,但它不起作用。它只删除了第一类.two

$e = $html ->find('.two, .three, .four');
$e->outertext = '';
echo $html ->outertext;

【问题讨论】:

    标签: php


    【解决方案1】:

    很抱歉给您带来了困惑。根据简单的 html dom 解析器文档,

    // Find all anchors and images with the "title" attribute
    $ret = $html->find('a[title], img[title]');
    
    <div class="mainnavs">
        <ul>
            <li>Tags</li>
            <li>Users</li>
        </ul>
    </div>
    
    <div class="askquestion">
        <ul>
            <li>
                s
            </li>
        </ul>
    </div> 
    

    上面的HTML我得到了预期的结果,

    $result = $html->find('[.mainnavs], [.askquestion]' , 0);
    

    【讨论】:

      【解决方案2】:

      它对我来说很好用。

      我在上面的代码中发现的问题就像你的变量名一样。您的内容在 $html 中,但您使用 $str 变量来查找 div。

      $html = str_get_html('<div><div class="two">two</div><div class="three">thomas</div><div    class="four">babu</div></div>');     
      $e = $html->find('#two , #three',0);
      $e->outertext = '';
      echo $html->outertext;
      

      请查看。

      【讨论】:

      • 这不是问题。这只是 SO 上的一个错字。这里'我的 php 文件看起来像eprogramers.com/domparser.txt。它不适合我。试试我有它,让我知道这是否适合你。
      • 嗨小指,我在将“$html->find('.two,.one',0);”更改为 $html->find('#two,#one', 0);它的工作。
      • 那行不通。你能把你的工作代码提供给我吗?
      • $html = new simple_html_dom(); $html->load('
        testing1
        testing4
        '); $e = $html->find('#two, #one',0); $e->outertext = ''; echo $html->外文;
      • 这不是您的工作代码。您的 html 加载中有类,但您在代码中使用了 id。我认为您尚未对此进行测试。
      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 2012-01-17
      • 1970-01-01
      相关资源
      最近更新 更多