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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

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