2013-03-04 95 views
0

我解析一个网页的一些数据没有问题。 我试图获得某些div的类名。PHP简单的HTML DOM解析器 - 打印类名称

例子:

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

我想在阵列只是b3保存。 可以做到这一点吗?

谢谢!

+0

重复? http://stackoverflow.com/questions/4835300/php-dom-to-get-tag-class-with-multiple-css-class-name – user1477388 2013-03-04 18:59:42

回答

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

+0

感谢您的回答。 我已经看到了这个解决方案,但是我不知道如何在Php Simple DOM库中使用它。 它可能与此库使用getAttribute或我只​​是不知道正确的sintax。 – MrRees 2013-03-04 19:28:09

+0

你应该能够从标签中抓取所有属性(比如'class'标签),像这样http://stackoverflow.com/questions/14456621/simple-html-dom-getting-all-attributes-from-a -tag示例显示'a'标记,但您应该可以修改以获取'class'标记。 **真的,你应该能够使用你当前的代码,并简单地'爆炸'的价值,以获得所有的类个别。** – user1477388 2013-03-04 19:50:00

+0

$ a-> attr这就是我正在寻找。 再次感谢! – MrRees 2013-03-04 22:11:50