0

Im newbie with DOM so can someone tell me how to parse the following in php?

<div class="classname1">
    <div class="description">some description</div>

    <div class="classname2">
    <div class="classname3">some text 1</div>
    <div class="classname4">some text 2</div>
    <div class="classname6">some text 4</div>
</div>
</div>

I would like to retrieve the text in the above class. There could be mode div before and after the html mentioned. I know I should create a dom

$dom = new DOMDocument();   
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);                
$divs = $xpath->query('//div[@class="classname1"]');    
foreach ($divs as $div) { 
    //...
}

I dont know how to access the classnames data

Josh
  • 8,082
  • 5
  • 43
  • 41
  • since you're looking for a single element, a better practice will be to use "id" instead of "class" – Nir Alfasi Feb 12 '12 at 08:16
  • possible duplicate of [Grabbing the href attribute of an A element](http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element) – Gordon Feb 12 '12 at 11:07
  • possible duplicate of [How to parse and process HTML with PHP](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) – Gordon Feb 12 '12 at 11:08

1 Answers1

0

you can use the getAttribute on the DOMElement http://www.php.net/manual/en/domelement.getattribute.php