2

I've got the following problem: I'm using the PHP XML DOM parser and when I'm parsing real-world HTML, the 'class' attributes of many elements have spaces in them, so there are actually multiple CSS classes for these elements. However, when I query the DOMNode with getAttribute(), I'm getting only the first class.

I have also tried PHP simplehtmldom for the very same purpose, but to the same result.

This question has been asked here before, however not really answered: PHP dom to get tag class with multiple css class name

This workaround does not work in my case. Any help would be appreciated :)

The other question contains a very good example of what I need to accomplish in case anyone did not understand me.

Community
  • 1
  • 1
Ger4ish
  • 137
  • 2
  • 5
  • there seems to be a working code example, have you tried that? – Ben Sep 19 '11 at 01:11
  • If you're referring to the code in the question itself, it only demonstrates the problem I'm trying to solve :( Sadly it does not work out the issue. – Ger4ish Sep 19 '11 at 01:16
  • no the code link in the comment that say's its working http://codepad.org/VZVUXgrT – Ben Sep 19 '11 at 01:22

1 Answers1

2

You can try this for the example in the stackoveflow question you linked to. Works for me:

$body = // the html
$html = new DomDocument();
$html->loadHTML($body);
$as = $html->getElementsByTagName('a');

foreach($as as $a) { 
    if ($a->getAttribute('class')=='secondLink SecondClass') {
          // do something
    }
 } 
Jeune
  • 3,498
  • 5
  • 43
  • 54
  • OK, I can't reproduce the correct behavior, but I'll check the configuration on my end. If I can't find anything, I'm accepting your answer – Ger4ish Sep 19 '11 at 13:24
  • Did you get it to work? What seems to be not working? Can you post the full code that you used? – Jeune Sep 20 '11 at 05:22