title: PHP - Issues with infinite looping
body: <p>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.</p>
  
  <pre><code>$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-&gt;loadHTML($output);
  libxml_use_internal_errors(false); //Start Showing Errors
  
  $xpath = new DOMXpath($doc);    
  
  foreach ($xpath-&gt;query('//h1') as $e) {
      $country = $e-&gt;nodeValue;
      $list = array();
      foreach ($xpath-&gt;query('./following-sibling::div[@class="colmask"]', $e) as $li) {
  
          foreach ($xpath-&gt;query('//div/h4', $e) as $div) {
              $state = $div-&gt;nodeValue;
  
  
              foreach ($xpath-&gt;query('./following-sibling::ul/li', $div) as $div2) {                      
  
                  $href =  $div2-&gt;getAttribute("href");
                  $text = trim(preg_replace("/[\r\n]+/", " ", $div2-&gt;nodeValue));
                  echo 'Country: ' . $country . ' State: ' . $state . ' CITY: text['. $text . '] href[' . $href . '] &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;';
  
  
              }
  
  
          }
  
      }
  }  
  </code></pre>