-1

I have a piece of HTML code that contains sub divs that have information I need to extract. I'm able to query for the parent div or specifically one of the child divs, but I can't seem to do it all at the same time.

For every instance of the <div class="box"> ... </div>

  1. I need to extract the text from the second element ("test text" in this case)
  2. I need to extract the text from the <div class="pBox"> ($230 in this case)

Note: the div id "adx_113_x_shorttext" is generally random for each instance but starts with "adx_".

Here is a sample of the HTML:

<div class="box">
  <div id="adx_113_x_shorttext" class="shorttext">test text</div>
  <div class="btnLst"><span id="vs_table_IRZ_x_MI52_x_c010_MI52" class="info">
     <flag class="tooltip-show-condition"></flag></span></div>
  <div class="pBox">
    <div>$230</div>
  </div>
</div>

I've tried the following PHP code but $acc_count isn't aligning well and I'm fairly certain this is not very efficient or the correct way:

$acc_count = 0;
$accessories = $xpath->query("//div[contains(@class,'shorttext')]");
$price = $xpath->query("//div[contains(@class,'pBox')]");
foreach ($accessories as $node) {
  echo $node->nodeValue . " | " . $price[$acc_count]->nodeValue; . "\n";
  $acc_count++;
}

Can someone show me the correct way to query the div box class and it's sub divs?

1 Answer 1

1

If I understand you correctly, this should get you there:

$accessories = $xpath->query("//div[contains(@class,'shorttext')]/text()");
$price = $xpath->query("//div[contains(@class,'pBox')]/div/text()");
$acc_count = 0;
foreach ($accessories as $node) {
  echo $node->nodeValue  . " | " . $price[$acc_count]->nodeValue . "\n";
  $acc_count++;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.