【问题标题】:PHP Simple HTML DOM parser - print class namePHP 简单 HTML DOM 解析器 - 打印类名
【发布时间】:2013-03-04 18:58:58
【问题描述】:

我从一个网页解析一些数据没有什么问题。 我正在尝试获取某些 div 的类名。

例子:

< div class="stars b3"></div>

我只想将b3 保存在数组中。 可以这样做吗?

谢谢!

【问题讨论】:

标签: php class parsing dom


【解决方案1】:

看这个:

<?php // http://stackoverflow.com/questions/4835300/php-dom-to-get-tag-class-with-multiple-css-class-name

$html = <<< HTML
<td class="pos" >
    <a class="firstLink" href="Search/?List=200003000112097&sr=1" >
        Firs link value
    </a>

    <br />

    <a class="secondLink SecondClass" href="/Search/?KeyOpt=ALL" >
        Second Link Value
    </a>
</td
HTML;

$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate(
    "/html/body//a[@class='secondLink SecondClass']"
);
echo $hrefs->item(0)->getAttribute('class');

参考。 http://codepad.org/VZVUXgrT

【讨论】:

  • 感谢您的回答。我已经看到了这个解决方案,但我不知道如何将它与 Php Simple DOM 库一起使用。它可以在这个库中使用 getAttribute 或者我只是不知道正确的 sintax。
  • 您应该能够从标签(例如class 标签)中获取所有属性,例如stackoverflow.com/questions/14456621/… 示例显示a 标签,但您应该能够修改以获得class 标签。 真的,您应该能够使用您当前的代码并简单地explode() 值来单独获取所有类。
  • $a->attr 这就是我想要的。再次感谢!
  • 不客气。不要忘记投票/接受对您有帮助的答案。
猜你喜欢
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 2014-01-15
  • 2011-08-27
  • 2018-03-05
  • 2015-02-14
相关资源
最近更新 更多