'How to extract price from given url using Xpath?

From a given url I want to extract some detail from it like title, price.

For the title, it works fine with this code:

<?php
    $getURL = file_get_contents('http://realestate.com.kh/residential-for-sale-in-phnom-penh-phnom-penh-bkk-1-1-bed-apartment-2386341');
    $dom = new DOMDocument();
    @$dom->loadHTML($getURL);
    $xpath = new DOMXPath($dom);

    echo $xpath->evaluate('//title')->item(0)->nodeValue."\n";
    
?>

But I wonder how to extract price($) using xpath? Is there any way to solve this?



Solution 1:[1]

echo $xpath->evaluate('//p[contains(@class,"price")]')->item(0)->nodeValue;

This will find <p class="price right s-cf">US$160000</p>

The contains is doing a wildcard search on the class attribute of the element.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vinzee