1

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?

MrTechie
  • 1,797
  • 4
  • 20
  • 36
  • 1
    `->find('foo')` is going to search for an element `` in your DOM. And how do your attempts not work? What are you doing with `$e2` inside those loops? – Marc B Jul 14 '14 at 18:04
  • When I print_r($e2) it's empty, and I am doing $e2->innertext and as a result - it's empty. – MrTechie Jul 14 '14 at 18:05
  • Both `div.pendingPartNumber` and `div[class=pendingPartNumber]` should work fine, I suppose. The question is what you're doing with `$e2` after that. What's returned by `var_dump($html->find('div.pendingPartNumber'))`? – raina77ow Jul 14 '14 at 18:06
  • Your 3rd attempt is the correct one. Try doing a `var_dump($html)` to make sure that your entire html snippet did get loaded/parsed properly. – Marc B Jul 14 '14 at 18:07
  • @raina77ow - array(0) { } – MrTechie Jul 14 '14 at 18:08
  • I did in fact dump the $html and it is loading properly. Plus it is in fact showing me the $vin dump as well - so I know it does get loaded. – MrTechie Jul 14 '14 at 18:19

1 Answers1

1
/* 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'); */
  • Can you try running the same code above ? as it worked for me, also please put error_reporting on and see if some error is coming ? – AbhishekTaneja Jul 14 '14 at 18:40