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.

3
  • there seems to be a working code example, have you tried that? Commented Sep 19, 2011 at 1: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. Commented Sep 19, 2011 at 1:16
  • no the code link in the comment that say's its working codepad.org/VZVUXgrT Commented Sep 19, 2011 at 1:22

1 Answer 1

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
    }
 } 
Sign up to request clarification or add additional context in comments.

2 Comments

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
Did you get it to work? What seems to be not working? Can you post the full code that you used?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.