0

I have little problem with parsing some data from one webpage. I'm trying to get class name of certain div.

Example:

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

I want to save in array just b3. Is it possible to do this?

Thanks!

N8TRO
  • 3,348
  • 3
  • 22
  • 40
MrRees
  • 1
  • 1
  • 1

1 Answers1

1

See this:

<?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');

Ref. http://codepad.org/VZVUXgrT

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • Thanks for your answer. I already saw this solution, but i don't know how to use this with Php Simple DOM library. Its possible to use getAttribute with this library or i just dont know right sintax. – MrRees Mar 04 '13 at 19:28
  • You should be able to grab all attributes from a tag (such as the `class` tag) like this http://stackoverflow.com/questions/14456621/simple-html-dom-getting-all-attributes-from-a-tag Example shows `a` tag, but you should be able to modify to get the `class` tag. **Really, you should just be able to use your current code and simply `explode()` the value to get all the classes individually.** – user1477388 Mar 04 '13 at 19:50
  • $a->attr this is what i was looking for. Thanks again! – MrRees Mar 04 '13 at 22:11