October 18, 2012

Retrieve posts from craigslist in PHP

Question by TapThatApp

I want to use file_get_contents to grab only information between the p
tags.

I’ve tried to use DOM elements but when I use getElementByTagName and loop through the array, it strips all HTML within the <p> tag.

<?php
function search(){
    $city = $_POST['city'];
    $scity = trim($city);
    $dom = new DOMDocument;
    libxml_use_internal_errors(true);

    $dom->loadHTMLFile('http://'.$scity.'.craigslist.org/search/cto?query=Toyota');
    libxml_use_internal_errors(false);
    $xpath = new DOMXPath($dom);
    $contents = $xpath->query('//p[@class="row"]');

    foreach($contents as $val){
        echo '<a href="' . $val->getElementsByTagName('a')->getAttribute('href') . '">' .  utf8_decode(trim($val->nodeValue, " -,")) . "</a><br />n";;
    }
}
?>

I want it to display just as it does on craigslist.
The title with the link to craigslist and price, img etc…

Answer by Starx

Use preg_match_all():

$text = file_get_contents("....");

preg_match_all('|<p>(.*?)</p>|',$texst, $matches); //Use regex to match all inside `p`

var_dump($matches); //View the matches
...

Please fill the form - I will response as fast as I can!