php xpath get contents of div with class

12,575

There is no HTML in the class="page-display" divs - so you're not going to get anything at all.

Do you mean the get class="parbase cn_text"?

    foreach($xpath->query('//div[@class="parbase cn_text"]') as $div){
        $num++;
        echo "$num. ";

        //????
        echo $div->textContent;

        echo "<br/>";
    }
Share:
12,575

Related videos on Youtube

David
Author by

David

Updated on September 28, 2022

Comments

  • David
    David over 1 year

    What is the right syntax to use xpath to get the contents of all divs with a certain class? i seem to be getting the divs but i don't know how to get their innerHTML.

            $url = "http://www.vanityfair.com/politics/2012/10/michael-lewis-profile-barack-obama";
    
        $ctx     = stream_context_create(array('http'=> array('timeout' => 10)));
    
        libxml_use_internal_errors(TRUE);
        $num = 0;
    
        if($html = @file_get_contents($url,false,$ctx)){
    
            $doc   = DOMDocument::loadHTML($html);
            $xpath = new DOMXPath($doc);
    
            foreach($xpath->query('//div[@class="page-display"]') as $div){
                $num++;
                echo "$num. ";
    
                //????
    
                echo "<br/>";
            }
    
            echo "<br/>FINISHED";
    
        }else{
            echo "FAIL";
        }
    
  • David
    David over 11 years
    wow, i'm stupid. That works, but how come the class "body" doesn't work?
  • Robbie
    Robbie over 11 years
    you mean "body "? ("body[space]")
  • David
    David over 11 years
    oh dang, spaces count? that's annoying. So if you had 4 divs with classes 1. "body test" 2. "test body example" 3. "example test body" 4. "body" you would need to query on "body ", " body ", " body", and "body"?
  • Robbie
    Robbie over 11 years
    And any combination that mixed with "body" - so best to use regular expressions as arr.gr/blog/2010/04/… suggests you can. Not tried it, though. Another example: stackoverflow.com/questions/10335736/xpath-query-with-regex