I have no clue what I am doing wrong. EVERY time it goes through it just keeps looping and pulling all of the cities listed in the same row and placing them with the state, then when it goes to the next state it starts at the correct place, but still keeps going. I've tried for 4 hours I just can't figure it out.
$url = 'http://www.craigslist.org/about/sites';
$output = file_get_contents($url);
$doc = new DOMDocument();
libxml_use_internal_errors(true); //Supress Warnings for HTML5 conversion issue
$doc->loadHTML($output);
libxml_use_internal_errors(false); //Start Showing Errors
$xpath = new DOMXpath($doc);
foreach ($xpath->query('//h1') as $e) {
$country = $e->nodeValue;
$list = array();
foreach ($xpath->query('./following-sibling::div[@class="colmask"]', $e) as $li) {
foreach ($xpath->query('//div/h4', $e) as $div) {
$state = $div->nodeValue;
foreach ($xpath->query('./following-sibling::ul/li', $div) as $div2) {
$href = $div2->getAttribute("href");
$text = trim(preg_replace("/[
]+/", " ", $div2->nodeValue));
echo 'Country: ' . $country . ' State: ' . $state . ' CITY: text['. $text . '] href[' . $href . '] <br/><br/><br/>';
}
}
}
}