phpsimpledom

extract div class simple dom


So I have a data structure that looks like this:

<h3>VIN: 1J8FA24177L168628</h3>
<div class="pendingPartDetail">
  <div class="pendingPartNumber">1BE95XXXAD</div>
  <div class="pendingPartDescription">FRONT BUMPER: Dam</div>
</div>
<div class="pendingPartDetail">
  <div class="pendingPartNumber">68003322AA</div>
  <div class="pendingPartDescription">FRONT BUMPER: Reinf beam</div>
</div>

I load it using simple dom:

   $html = str_get_html($store2);

When I look for the vin - I am in fact able to find it like this:

     foreach($html->find('h3') as $e){
             $vin = trim($e->innertext);
                 echo $vin . '<br>';
      } 

But when I try to get the pendingPartNumber or the pendingPartDescription - it won't extract the data that I need. These are the alternatives I have tried:

foreach($html->find('pendingPartNumber') as $e2){

foreach($html->find('div.pendingPartNumber') as $e2){

foreach($html->find('div[class=pendingPartNumber]') as $e2){

None of those seems to extract the data within the element. What am I missing in regards to properly extracting the data?


Solution

  • /* This should work */
    
    $store2 = '<h3>VIN: 1J8FA24177L168628</h3><div class="pendingPartDetail"><div class="pendingPartNumber">1BE95XXXAD</div><div class="pendingPartDescription">FRONT BUMPER: Dam</div></div><div class="pendingPartDetail"><div class="pendingPartNumber">68003322AA</div><div class="pendingPartDescription">FRONT BUMPER: Reinf beam</div></div>';
    
        $html = str_get_html($store2);
    
        foreach($html->find('div[class=pendingPartNumber]') as $e2){
            var_dump($e2 -> innertext);
        }
    
        /* If you are using it in a file, then use file_get_html('somefile.html'); */