0

I have an HTML block here:

<div class="title">
    <a href="http://test.com/asus_rt-n53/p195257/">
        Asus RT-N53
    </a>
</div>
<table>
    <tbody>
        <tr>
            <td class="price-status">
                <div class="status">
                    <span class="available">Yes</span>
                </div>
                <div name="price" class="price">
                    <div class="uah">758<span> ua.</span></div>
                    <div class="usd">$&nbsp;62</div>
                </div>

How do I parse the link (http://test.com/asus_rt-n53/p195257/), title (Asus RT-N53) and price (758)?

Curl code here:

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$models = $xpath->query('//div[@class="title"]/a');
foreach ($models as $model) {
    echo $model->nodeValue;
    $prices = $xpath->query('//div[@class="uah"]');
    foreach ($prices as $price) {
        echo $price->nodeValue;
    }
}
j0k
  • 22,600
  • 28
  • 79
  • 90
Dima
  • 11
  • 1
  • 5
  • 1
    What's the problem with your current code? – Felix Kling Jan 10 '13 at 21:28
  • so I get a name and price, but I need more and a link. and such units on a page is not much, as it can be in a loop for to do? and that now looks like -Name -Price - ....... -Price and it is necessary: -Name -Price -link – Dima Jan 10 '13 at 21:36
  • 1
    You have to read the `href` attribute. Maybe this helps: http://stackoverflow.com/questions/6856668/domdocument-read-tag-attributes-classes. – Felix Kling Jan 10 '13 at 21:47

1 Answers1

0

One ugly solution is to cast the price result to keep only numbers:

echo (int) $price->nodeValue;

Or, you can query to find the span inside the div, and remove it from the price (inside the prices foreach):

$span = $xpath->query('//div[@class="uah"]/span')->item(0);
$price->removeChild($span);
echo $price->nodeValue;

Edit:

To retrieve the link, simply use getAttribute() and get the href one:

$model->getAttribute('href')
j0k
  • 22,600
  • 28
  • 79
  • 90