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/>";
}
Related videos on Youtube
Author by
David
Updated on September 28, 2022Comments
-
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 over 11 yearswow, i'm stupid. That works, but how come the class "body" doesn't work?
-
Robbie over 11 yearsyou mean "body "? ("body[space]")
-
David over 11 yearsoh 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 over 11 yearsAnd 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